我发送一个错误响应到我的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 ! ");
}
});
现在我的结果是:
log:
[XMLHttpRequest readyState=4 status=500, "error", undefined]
警告:
不能做是因为:错误
什么好主意吗?
犯错。responseText包含HTML标签,你可以很容易地从这些标签得到错误消息…
例如:
$(err.responseText)
// k.fn.init(12) [text, title, text, meta, text, style, text, span, text, font, text, //comment]
$.ajax({
async: bAsync,
type: 'POST',
url: pUrl,
contentType: 'application/json; charset=utf-8;',
dataType: 'json',
data: pData,
success: fn,
error: function(err) {
alert( $($(err.responseText)[1]).text() )
debugger;
}
});
犯错。responseText包含HTML标签,你可以很容易地从这些标签得到错误消息…
例如:
$(err.responseText)
// k.fn.init(12) [text, title, text, meta, text, style, text, span, text, font, text, //comment]
$.ajax({
async: bAsync,
type: 'POST',
url: pUrl,
contentType: 'application/json; charset=utf-8;',
dataType: 'json',
data: pData,
success: fn,
error: function(err) {
alert( $($(err.responseText)[1]).text() )
debugger;
}
});