ajax 有时成功有时失败,jQuery.ajax()成功/失败时调用回调?

我觉得你可以在github上线394看到这个jQuery代码和:

在取决于您收到主要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;

}

}

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值