admin管理员组文章数量:1129246
I am sending an error response to my jQuery. However, I can not get the response text (in the example below this would be Gone to the beach)
The only thing jQuery says is 'error'.
See this example for details:
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 ! ");
}
});
Now my result ist:
log:
[XMLHttpRequest readyState=4 status=500, "error", undefined]
alert:
Can't do because: error
Any ideas?
I am sending an error response to my jQuery. However, I can not get the response text (in the example below this would be Gone to the beach)
The only thing jQuery says is 'error'.
See this example for details:
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 ! ");
}
});
Now my result ist:
log:
[XMLHttpRequest readyState=4 status=500, "error", undefined]
alert:
Can't do because: error
Any ideas?
Share Improve this question edited Feb 9, 2015 at 15:54 robsch 9,66810 gold badges68 silver badges107 bronze badges asked Oct 28, 2009 at 12:40 jantimonjantimon 38.1k23 gold badges126 silver badges192 bronze badges 2 |13 Answers
Reset to default 353Try:
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
For me, this simply works:
error: function(xhr, status, error) {
alert(xhr.responseText);
}
Look at the responseText
property of the request parameter.
As ultimately suggested by this other answer and its comments on this page:
error: function(xhr, status, error) {
var err = JSON.parse(xhr.responseText);
alert(err.Message);
}
The best simple approach :
error: function (xhr) {
var err = JSON.parse(xhr.responseText);
alert(err.message);
}
This is what worked for me
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);
}
}
If you want to get Syntax Error with line number, use this
error: function(xhr, status, error) {
alert(error);
}
you can try it too:
$(document).ajaxError(
function (event, jqXHR, ajaxSettings, thrownError) {
alert('[event:' + event + '], [jqXHR:' + jqXHR + '], [ajaxSettings:' + ajaxSettings + '], [thrownError:' + thrownError + '])');
});
This will allow you to see the whole response not just the "responseText" value
error: function(xhr, status, error) {
var acc = []
$.each(xhr, function(index, value) {
acc.push(index + ': ' + value);
});
alert(JSON.stringify(acc));
}
I used this, and it worked perfectly.
error: function(xhr, status, error){
alertify.error(JSON.parse(xhr.responseText).error);
}
Try this to have complete error detail in console.
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("Error Thrown: " + errorThrown);
console.log("Text Status: " + textStatus);
console.log("XMLHttpRequest: " + XMLHttpRequest);
console.warn(XMLHttpRequest.responseText)
}
If you're not having a network error, and wanting to surface an error from the backend, for exmple insufficient privileges, server your response with a 200 and an error message. Then in your success handler check data.status == 'error'
err.responseText
contain HTML tags you can get error message from these tags easily...
For example:
$(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;
}
});
本文标签: phpHow to get the jQuery ajax error response textStack Overflow
版权声明:本文标题:php - How to get the jQuery $.ajax error response text? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736715188a1949160.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
header
function handle this? – rfunduk Commented Oct 28, 2009 at 12:42$.ajax()
calls the error function passed to it. – Chris Charabaruk Commented Oct 28, 2009 at 13:49