javascript jQuery.ajax() 成功/失败回调何时调用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4013283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery.ajax() success/failure callbacks called when?
提问by bjornl
I've been going through the source to find out the critiera for jQuery.ajax()'s success/failure methods being called. It is not based solelyon the status code, it seems to also involve the data type.
我一直在查看源代码,以找出调用 jQuery.ajax() 的成功/失败方法的标准。它不是基于纯粹的状态代码,它似乎还涉及到数据类型。
I always resort to writing custom error handlers using the 'complete'-callback.
我总是求助于使用“完整”回调编写自定义错误处理程序。
Exactly which are the critera for the success/failure calls?
究竟哪些是成功/失败调用的标准?
采纳答案by Nick Craver
As you said, it depends on the data type, scriptis a special one for instance, the check is:
正如您所说,这取决于数据类型,script例如特殊类型,检查是:
- Has the request alreadycompleted? (don't fire twice)
- Is the
readyState"loaded" or "complete"?
- 请求是否已经完成?(不要开火两次)
- 是
readyState“加载”还是“完成”?
For other requests it's checks the following:
对于其他请求,它会检查以下内容:
- Is it a timeout?
Does
jQuery.httpSuccess()return true?Is it modified? (do we care about the [not] updated result?)
- 是超时吗?
是否
jQuery.httpSuccess()返回true?修改了吗?(我们关心 [not] 更新的结果吗?)
Note:The above is for jQuery 1.4.3, jQuery 1.4.2 and below had an additional "success" scenario where a response code of 0was also "successful", this was done because Opera returns a 0when it's reallya 304. This is incorrect behavior, and the jQuery team opted to drop support for this quirk, since it caused false-positives in other actual 0response code cases.
注意:以上适用于 jQuery 1.4.3,jQuery 1.4.2 及以下版本有一个额外的“成功”场景,其中 的响应代码0也是“成功”,这样做是因为 Opera 返回 a0当它真的是304. 这是不正确的行为,jQuery 团队选择放弃对这个 quirk 的支持,因为它在其他实际0响应代码案例中导致误报。
回答by netadictos
I think you can see this in the jquery code in github line 394 and on:
我想你可以在 github line 394 和 on 的 jquery 代码中看到这一点:
http://github.com/jquery/jquery/blob/master/src/ajax.js
http://github.com/jquery/jquery/blob/master/src/ajax.js
In depends on the readyState code you receive mainly and a variable where it controls the timeout:
In 取决于您主要收到的 readyState 代码和一个控制超时的变量:
var onreadystatechange = xhr.onreadystatechange = function( isTimeout ) {
// The request was aborted
if ( !xhr || xhr.readyState === 0 || isTimeout === "abort" ) {
// Opera doesn't call onreadystatechange before this point
// so we simulate the call
if ( !requestDone ) {
jQuery.handleComplete( s, xhr, status, data );
}
requestDone = true;
if ( xhr ) {
xhr.onreadystatechange = jQuery.noop;
}
// The transfer is complete and the data is available, or the request timed out
} else if ( !requestDone && xhr && (xhr.readyState === 4 || isTimeout === "timeout") ) {
requestDone = true;
xhr.onreadystatechange = jQuery.noop;
status = isTimeout === "timeout" ?
"timeout" :
!jQuery.httpSuccess( xhr ) ?
"error" :
s.ifModified && jQuery.httpNotModified( xhr, s.url ) ?
"notmodified" :
"success";
var errMsg;
if ( status === "success" ) {
// Watch for, and catch, XML document parse errors
try {
// process the data (runs the xml through httpData regardless of callback)
data = jQuery.httpData( xhr, s.dataType, s );
} catch( parserError ) {
status = "parsererror";
errMsg = parserError;
}
}
// Make sure that the request was successful or notmodified
if ( status === "success" || status === "notmodified" ) {
// JSONP handles its own success callback
if ( !jsonp ) {
jQuery.handleSuccess( s, xhr, status, data );
}
} else {
jQuery.handleError( s, xhr, status, errMsg );
}
// Fire the complete handlers
if ( !jsonp ) {
jQuery.handleComplete( s, xhr, status, data );
}
if ( isTimeout === "timeout" ) {
xhr.abort();
}
// Stop memory leaks
if ( s.async ) {
xhr = null;
}
}
};

