jQuery's AJAX error function has the following parameters:
jQuery的AJAX错误函数有以下参数:
error(XMLHttpRequest, textStatus, errorThrown)
What's the best cross-browser way to get the response body?
获得响应主体的最佳跨浏览器方式是什么?
Does this work (reliably in all browsers)?
这是否有效(在所有浏览器中都可靠)?
$.ajax({
error: function(http) {
alert(http.responseText);
}
});
4 个解决方案
#1
-5
There is a hidden function that can extract the data from XHR istance:
有一个隐藏函数可以从XHR istance中提取数据:
var responseText = $.httpData(xhr)
If you pass "json"
as a second parameter it will treat the response as a JSON string.
如果您将“json”作为第二个参数传递,它将把响应视为json字符串。
Note that you might get an error because there is no response (network problem for example). Make sure you cover that case as well. Also, I believe (not sure) that jQuery invokes the error
handler if the server returns a 4xx
or 5xx
status.
注意,您可能会得到一个错误,因为没有响应(例如网络问题)。确保你也涵盖了这个案例。另外,我认为(不确定)如果服务器返回4xx或5xx状态,jQuery将调用错误处理程序。
#2
73
As of jQuery 1.4.1 you should use:
对于jQuery 1.4.1,您应该使用:
var json = JSON.parse(xhr.responseText);
See http://api.jquery.com/jQuery.parseJSON/.
见http://api.jquery.com/jQuery.parseJSON/。
#3
17
For a more recent and general answer (since jquery 1.5), I'd use the jqXHR
object:
对于更近期和更一般的答案(自jquery 1.5以来),我将使用jqXHR对象:
$.ajax(url).fail(function(jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText);
})
Alternatively responseJSON
can be used to get the response body already parsed
还可以使用responseJSON获取已经解析的响应体
$.ajax(url).fail(function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR.responseJSON);
})
#4
0
One straightforward usage example with jQuery:
jQuery一个简单的用法示例:
var url = '/';
$.get(url).then(
function(response) {
$("#result").html(response);
},
function(jqXHR) {
$("#result").html('Error occurred: '+ jqXHR.statusText + ' ' + jqXHR.status);
}
);
This should return the HTML of current website front page.
这应该返回当前网站首页的HTML。
Then try entering a nonsense URL that doesn't exist and see the error thrown. You should get "404 Not Found" from web server. Test it: JSFiddle here
然后尝试输入一个不存在的无意义URL并查看抛出的错误。您应该从web服务器获得“404 Not Found”。测试:JSFiddle这里
#1
-5
There is a hidden function that can extract the data from XHR istance:
有一个隐藏函数可以从XHR istance中提取数据:
var responseText = $.httpData(xhr)
If you pass "json"
as a second parameter it will treat the response as a JSON string.
如果您将“json”作为第二个参数传递,它将把响应视为json字符串。
Note that you might get an error because there is no response (network problem for example). Make sure you cover that case as well. Also, I believe (not sure) that jQuery invokes the error
handler if the server returns a 4xx
or 5xx
status.
注意,您可能会得到一个错误,因为没有响应(例如网络问题)。确保你也涵盖了这个案例。另外,我认为(不确定)如果服务器返回4xx或5xx状态,jQuery将调用错误处理程序。
#2
73
As of jQuery 1.4.1 you should use:
对于jQuery 1.4.1,您应该使用:
var json = JSON.parse(xhr.responseText);
See http://api.jquery.com/jQuery.parseJSON/.
见http://api.jquery.com/jQuery.parseJSON/。
#3
17
For a more recent and general answer (since jquery 1.5), I'd use the jqXHR
object:
对于更近期和更一般的答案(自jquery 1.5以来),我将使用jqXHR对象:
$.ajax(url).fail(function(jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText);
})
Alternatively responseJSON
can be used to get the response body already parsed
还可以使用responseJSON获取已经解析的响应体
$.ajax(url).fail(function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR.responseJSON);
})
#4
0
One straightforward usage example with jQuery:
jQuery一个简单的用法示例:
var url = '/';
$.get(url).then(
function(response) {
$("#result").html(response);
},
function(jqXHR) {
$("#result").html('Error occurred: '+ jqXHR.statusText + ' ' + jqXHR.status);
}
);
This should return the HTML of current website front page.
这应该返回当前网站首页的HTML。
Then try entering a nonsense URL that doesn't exist and see the error thrown. You should get "404 Not Found" from web server. Test it: JSFiddle here
然后尝试输入一个不存在的无意义URL并查看抛出的错误。您应该从web服务器获得“404 Not Found”。测试:JSFiddle这里