如果我的PHP腳本返回錯誤,則我想要執行的操作是向用戶alert()一條錯誤消息。有人能告訴我如何做到這一點?如果我的服務器代碼返回false,如何回顯錯誤
0
A
回答
0
編輯:
所以我會做的是簡單地使用JSON並返回與例如消息200響應
PHP
$validationErrors = array();
foreach($data as $name => $value){
// validate each value, if its not valid append a message to the $validationErrors array
// like $validationErrors[$name] = $message;
}
if(!empty($validationErrors)){
// send json headers so jquery can parse properly
header('Content-type: application/json');
echo json_encode('valid'=>false, 'errors' => $validationErrors));
exit;
} else {
// send json headers so jquery can parse properly
header('Content-type: application/json');
echo json_encode(array('valid'=>true));
exit;
}
的jQuery:
$.ajax({
url: '/url/for/whatever.php',
data: {'my': 'data'},
dataType: 'json',
success: function(response){
if(!response.valid){
$.each(response.errors, function(name, msg){
var errmsg = $('<div class="error"></div>').text(msg);
$('[name='+name+']').before(errmsg);
});
alert('Errors were found!');
}
}
});
這樣你也可以顯示e爲每個字段提供反饋,允許用戶更正它們。你可能需要修改我的錯誤信息插入,但你明白了。
假設你的意思使用$.ajax
你會簡單的分配誤差函數:
$.ajax({
url: '/url/for/whatever.php',
data: {'my': 'data'},
error: function(){
alert('An error occurred!');
}
success: function(response){
// whatever happens if the response is successful
}
});
然而,對於要調用的誤差函數的服務器必須使用400或500系列錯誤代碼進行響應。例如,如果你正在處理你的php中的錯誤,然後輸出錯誤消息,那麼可以假設你將發送200響應,所以成功函數仍然會被調用。如果需要,可以使用php header()
函數手動設置標題來操作。
0
相關問題
- 1. 如果類型錯誤,返回False
- 2. mysql服務器返回錯誤結果
- 3. 如何從服務中返回錯誤?
- 4. 開發服務器返回的響應錯誤代碼:500
- 5. 服務器返回錯誤的HTTP響應。代碼500
- 6. 什麼是錯誤代碼35,由telegram.org服務器返回
- 7. Rails:如果before_update回調返回false,我如何取消保存?
- 8. 如果isset返回false
- 9. 返回值與返回錯誤代碼?
- 10. htaccess reditect如果服務器返回404
- 11. MKReverseGeocoder服務器返回的錯誤:503
- 12. isSelected返回false和器isChecked顯示在上面的代碼編譯錯誤
- 13. PHP array_key_exists返回false錯誤
- 14. 遠程服務器返回錯誤:NotFound
- 15. SOAP返回「內部服務器錯誤」
- 16. 遠程服務器返回錯誤:NotFound
- 17. 遠程服務器返回錯誤:NOTFOUND
- 18. Smtp服務器不返回錯誤
- 19. Google GCM服務器返回404錯誤
- 20. ChefServer - 服務器返回錯誤502
- 21. 服務器返回錯誤503
- 22. 獲取web服務返回的實際錯誤代碼
- 23. FTP上傳代碼,返回來自服務器的任何錯誤響應
- 24. Apache Web服務器 - 如何返回404錯誤,而不是403
- 25. HttpWebResponse返回遠程服務器返回錯誤:(403)禁止
- 26. 如何從servlet過濾器返回HTTP錯誤代碼?
- 27. 如何迭代Mongoose返回的錯誤?
- 28. FileSystemObject.FileExists返回false,即使我的地圖服務器路徑
- 29. 其他郵政服務返回錯誤代碼415
- 30. QMutexLocker是否返回錯誤代碼(如果有)?
你目前有什麼代碼? – 2010-10-13 06:03:45