问题描述
我广泛使用$ .get,$.post和$ .getJSON,所以我想看看.ajaxError是如何工作的.我试过了...
I use $.get, $.post, and $.getJSON extensively, so I wanted to see how .ajaxError worked. I tried this...
$(document).ajaxError(function (e, xhr, options, error)
{
console.log(e);
console.log(xhr);
console.log(options);
console.log(error);
});
,我也尝试将其放入我的$(document).ready().
中.然后我禁用了我的网络连接.从AJAX中的定时POST
冒出了很多错误(因为它们得到了意外的响应),并且GET
瞬间返回了(缓存),但是Firebug中没有其他内容,就像ajaxError从未出现过一样被呼叫.我究竟做错了什么?我只是再次尝试了,没有控制台日志,只是alert('foo')
,再次没有任何反应.错误很多,但是ajaxError永远不会触发.
and I also tried putting it inside my $(document).ready().
. And then I disabled my network connection. A slew of errors flew up from the timed POST
s in AJAX (because they were getting unexpected responses), and the GET
s were coming back instantaneously (cached), but nothing else appeared in Firebug, as if ajaxError were never being called. What am I doing wrong? I just tried again with no console logs and just alert('foo')
, and again nothing happened. Lots of errors, but ajaxError never firing.
推荐答案
ajaxError
github.com/jquery/jquery/blob/master/src/ajax.js#L636"rel =" noreferrer>在jQuery源中可以看到.该特定功能中的响应代码被视为成功"代码;任何其他事情都是失败.
ajaxError
is triggered when the actual XHR does not receive an HTTP success code from a given response from the server, as you can see in the jQuery source. The response codes in that particular function are perceived as "success" codes; anything else is a failure.
jQuery也仅分支到XMLHTTPRequest结束时以readyState
为4触发ajaxError
的代码(完成所有接收到的数据),因此我认为关闭网络连接实际上会使您陷入另一种情况总共XHR条件(可能是条件2的循环?). 您也可以看到jQuery在源代码中的分支方式
jQuery also only branches to the code that triggers ajaxError
when your XMLHTTPRequest winds up with a readyState
of 4 (complete with all data received), so I think shutting off your network connection would actually put you in a different XHR condition altogether (possibly a loop of condition 2?). You can see how jQuery branches in the source, too.
基本上,它看起来服务器是否没有说请求失败并发送了超出该范围的代码,您可能不会看到ajaxError
被触发,因为根据jQuery,它没有收到一个错误.
Basically, it looks if the server doesn't say the request failed and send back a code outside of that range above, you likely won't see ajaxError
being triggered as, according to jQuery, it hasn't received an error.
这篇关于$ .ajaxError如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!