获得Ajax响应机构在错误回调的使用 [英] Getting AJAX response body for use in error callback

查看:142
本文介绍了获得Ajax响应机构在错误回调的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery的AJAX误差函数具有下列参数:

 错误(XMLHtt prequest,textStatus,errorThrown)
 

什么是最好的跨浏览器的方式来获得响应主体?

请问这项工作(可靠在所有浏览器)?

  $。阿贾克斯({
  错误:功能(HTTP){
    警报(http.responseText);
  }
});
 

解决方案

有一个隐藏的功能,可以从XHR istance提取数据:

  VAR的responseText = $ .httpData(XHR)
 

如果您通过JSON作为第二个参数,它将把响应作为JSON字符串。

请注意,你可能会得到一个错误,因为没有反应(例如网络问题)。请确保您覆盖这种情况下也是如此。另外,我认为(不知道)的jQuery的调用错误处理程序,如果服务器返回一个 4XX 5XX 状态。

jQuery's AJAX error function has the following parameters:

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);
  }
});

解决方案

There is a hidden function that can extract the data from XHR istance:

var responseText = $.httpData(xhr)

If you pass "json" as a second parameter it will treat the response as a JSON string.

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.

这篇关于获得Ajax响应机构在错误回调的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆