获得Ajax响应机构在错误回调的使用回调、错误、机构、Ajax

由网友(Read a hell 一念地狱)分享简介:jQuery的AJAX误差函数具有下列参数:错误(XMLHtt prequest,textStatus,errorThrown)什么是最好的跨浏览器的方式来获得响应主体?请问这项工作(可靠在所有浏览器)?$。阿贾克斯({错误:功能(HTTP){警报(http.responseText);}});解决方案 有一个隐藏...

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

 错误(XMLHtt prequest,textStatus,errorThrown)
 

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

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

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

解决方案

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

  VAR的responseText = $ .httpData(XHR)
 
ajax报错get,使用七牛CDN导致ajax评论报错 error get from image source failed E405 落格博客...

如果您通过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.

阅读全文

相关推荐

最新文章