JQuery的AJAX的成功发射,因为.done回调的两倍,两倍、回调、JQuery、AJAX

2023-09-10 22:11:11 作者:做作、

我有一些遗留的code,从而中止的情形及放Ajax请求;然后用户响应之后,重新发送请求。

 函数resendLastAjaxRequest(ajaxRequest,ajaxSettings){
    如果(ajaxSettings!=未定义){
        $阿贾克斯(ajaxSettings).done(功能(数据){
            ajaxSettings.success(数据);
        });
    }
 

现在,上面的code类火灾的成功处理程序两次请求,但是它已经存在了一年多的时间和放大器;没有人遇到过。是因为jQuery版本更新吧。目前,我们正在使用2.0.3 我不知道什么时候被完全jQuery的更新,但可能会在早期版本的完成被解雇,而不是成功。 做了一些挖掘,但找不到任何理由为什么它可能已经工作了这么长时间,请大家帮忙。

更新

what是成功和.done()的$就

方法之间的差异

它包含评论说

  

好了,这是jQuery的1.8 :)因为$就从jQuery的1.5返回一个承诺   这是一个简单的替代的一致性的问题(使用   递延接口):做()发生成功的(),失败()的   错误(),总是()为完整的()

这意味着,如果完成被调用,那么我们就来触发成功明确。这可能是为什么它的工作了这么久的原因。但是现在,以前做过触发成功处理程序已经称为放大器;那么完成回调调用成功处理了。

如何改变/修改code,使得成功只调用一次有什么建议?

修改

ajaxSettings看起来是这样的:

 接受:对象
异步:真
缓存:假的
完成:功能(请求,状态)
的contentType:应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8
内容:对象
转换器:对象
跨域:假的
的dataType:数组[1]
错误:函数(请求,状态){flatOptions:全球对象:真hasContent:假isLocal:假JSONP:回调
jsonpCallback:函数(){VAR E = hn.pop()|| x.expando +_+ YT ++;返回此[E] = 0,E!} loadingdisplay:trueprocessData:真
responseFields:对象
成功:函数(htmlData){类型:GET
 
jQuery之Ajax

解决方案

就像我在评论中说,我认为,previously你有一个的jQuery 版本的低于1.5 (不含 .done()实现),所以成功的方法被调用的回调则 .done()扔在控制台的错误不会破坏任何东西(如不确定是不是一个函数)。所以在这种情况下,必须在控制台的错误,只有一个回调调用的函数。

演示与的jQuery 1.4.4 :的jsfiddle

在控制台:

  

1误差:不确定是不是一个函数(.done的,因为()

  

1日志:对象(从呼叫DATAS)

I have some legacy code, which aborts ajax requests in a scenario & then after user-response, resends that request.

function resendLastAjaxRequest(ajaxRequest, ajaxSettings){
    if(ajaxSettings != undefined){
        $.ajax(ajaxSettings).done(function ( data ) {
            ajaxSettings.success(data);
        });
    }

Now, the above code fires the success handler for the request twice, but's its been there for more than a year & no one encountered it. Is it because of jquery version updates. We're currently using 2.0.3 I don't know when exactly was jquery updated, but may be in earlier versions done was fired instead of success. Did some digging but couldn't find any reason why it might have worked for so long, please help.

Update

what is difference between success and .done() method of $.ajax

It contains a comment saying

ok, it's jQuery 1.8 :) Since $.ajax return a promise from jQuery 1.5 this is a simple substitution for a matter of consistency (using the interface of deferred): done() take place of success(), fail() for error() and always() for complete()

that means if done is called, then we have to trigger success explicitly. This might be the reason why it worked for so long. But now, before the done fires the success handler is already called & then the done callback calls the success handler again.

Any suggestions on how to change/modify the code such that success is called only once ?

EDIT

ajaxSettings looks like this :

accepts: Object
async: true
cache: false
complete: function (request, status)
contentType: "application/x-www-form-urlencoded; charset=UTF-8"
contents: Object
converters: Object
crossDomain: false
dataTypes: Array[1]
error: function (request, status){flatOptions: Object global: true hasContent: false isLocal: false jsonp: "callback"
jsonpCallback: function (){var e=hn.pop()||x.expando+"_"+Yt++;return this[e]=!0,e}loadingdisplay: trueprocessData: true
responseFields: Object
success: function (htmlData) {type: "GET"

解决方案

Like I said in the comments, I think previously you had a jQuery version lower than 1.5 (without .done() implemented) so the success method is invoked as a callback then .done() throw an error in the console without breaking anything (like undefined is not a function). So in this case, you have an error in the console and only one callback function called.

Demo with jQuery 1.4.4 : jsfiddle

In the console :

1 error : undefined is not a function (because of .done())

and

1 log : Object (datas from the call)

 
精彩推荐
图片推荐