如何获得jQuery$.ajax错误响应文本?我将向jQuery发送错误响应。但是,我无法获得响应文本(在下面的示例中,这将是去海滩)jQuery唯一说的是“错误”。有关详细信息,请参阅此示例:PHP<?
header('HTTP/1.1 500 Internal Server Error');
print "Gone to the beach"?>jQuery$.ajax({
type: "post",
data: {id: 0},
cache: false,
url: "doIt.php",
dataType: "text",
error: function (request, error) {
console.log(arguments);
alert(" Can't do because: " + error);
},
success: function () {
alert(" Done ! ");
}});现在我的结果列表:日志: [XMLHttpRequest readyState=4 status=500, "error", undefined]警报:不能做,因为:错误有什么想法吗?
3 回答
温温酱
TA贡献1752条经验 获得超4个赞
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);}
慕标琳琳
TA贡献1830条经验 获得超9个赞
function showErrorMessage(xhr, status, error) {
if (xhr.responseText != "") {
var jsonResponseText = $.parseJSON(xhr.responseText);
var jsonResponseStatus = '';
var message = '';
$.each(jsonResponseText, function(name, val) {
if (name == "ResponseStatus") {
jsonResponseStatus = $.parseJSON(JSON.stringify(val));
$.each(jsonResponseStatus, function(name2, val2) {
if (name2 == "Message") {
message = val2;
}
});
}
});
alert(message);
}
}添加回答
举报
0/150
提交
取消
