jQuery.ajax()成功/失败回调什么时候调用?

时间:2022-10-15 20:06:31

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 solely on 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?

究竟哪个是成功/失败电话的标准?

2 个解决方案

#1


11  

As you said, it depends on the data type, script is a special one for instance, the check is:

正如你所说,它取决于数据类型,例如脚本是一个特殊的,检查是:

For other requests it's checks the following:

对于其他请求,它会检查以下内容:

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 0 was also "successful", this was done because Opera returns a 0 when it's really a 304. This is incorrect behavior, and the jQuery team opted to drop support for this quirk, since it caused false-positives in other actual 0 response code cases.

注意:以上是针对jQuery 1.4.3,jQuery 1.4.2及更低版本有一个额外的“成功”场景,其中响应代码为0也是“成功”,这是因为当Opera真的是304时,Opera返回0。这是不正确的行为,jQuery团队选择放弃对这个怪癖的支持,因为它在其他实际的0响应代码案例中引起了误报。

#2


0  

I think you can see this in the jquery code in github line 394 and on:

我想你可以在github第394行的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:

取决于您主要接收的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;
}
}
};

#1


11  

As you said, it depends on the data type, script is a special one for instance, the check is:

正如你所说,它取决于数据类型,例如脚本是一个特殊的,检查是:

For other requests it's checks the following:

对于其他请求,它会检查以下内容:

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 0 was also "successful", this was done because Opera returns a 0 when it's really a 304. This is incorrect behavior, and the jQuery team opted to drop support for this quirk, since it caused false-positives in other actual 0 response code cases.

注意:以上是针对jQuery 1.4.3,jQuery 1.4.2及更低版本有一个额外的“成功”场景,其中响应代码为0也是“成功”,这是因为当Opera真的是304时,Opera返回0。这是不正确的行为,jQuery团队选择放弃对这个怪癖的支持,因为它在其他实际的0响应代码案例中引起了误报。

#2


0  

I think you can see this in the jquery code in github line 394 and on:

我想你可以在github第394行的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:

取决于您主要接收的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;
}
}
};