【发布时间】:2017-05-16 15:32:59
【问题描述】:
我当前的脚本仅在执行失败时处理带有模糊和一般错误消息的错误,但我希望它返回各种错误消息。我找到的许多其他答案与我已经拥有的答案相似,我无法在这里做我想做的事情。
当前的 AJAX(我需要更改的内容):
$.ajax(
{
url: './ajax.php',
data: {value1: 'test', value2: 'test', isChecked: isChecked},
dataType: 'json',
success: this.exeSuccess,
error: this.exeError
});
this.exeSuccess = function(){
var currentRow = $(this).closest('tr');
// Highlights table row to denote successful execution
currentRow.toggleClass('colorcode');
};
this.exeError = function(){
alert("Error. Please try again.");
return false;
};
当前 PHP:
if (isset($_GET['value1']) && isset($_GET['value2']) && isset($_GET['isChecked'])) {
// Execution logic here... if it fails, the whole script terminates
echo json_encode("");
}
我打算用 PHP 做什么来向 AJAX 返回详细的错误消息:
if (isset($_GET['value1']) && isset($_GET['value1']) && isset($_GET['isChecked'])) {
$errorMessage = "";
if (isset($_GET['isChecked']) == 'false'){
$errorMessage = "The check box is false";
} else {
$errorMessage = "The check box is true";
}
echo json_encode("$errorMessage"); // But how I handle it in the AJAX error:?
}
如何获取“AJAX 错误:”中处理的错误消息,以便执行不同的 alert([error_message])?
【问题讨论】:
-
如果 ajax 回调的 php 中有错误代码,就会发生
Ajax Error:事情。在 php 中适当地处理错误以将您检测到的标志作为 ajax 的响应发送回您的 js,如果没有返回该错误,则采取不同的行动。 -
如果要触发
error()方法,需要返回带有错误代码的响应。类似:http_response_code(422);在回显 json 响应之前应该可以工作。
标签: javascript php jquery json ajax