简体   繁体   English

由于.done回调,JQuery ajax成功触发了两次

[英]JQuery ajax success fired twice, because of .done callback

I have some legacy code, which aborts ajax requests in a scenario & then after user-response, resends that request. 我有一些旧代码,在场景中中止ajax请求,然后在用户响应后重新发送该请求。

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. 是因为jquery版本更新。 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 . 我们目前使用的是2.0.3,我不知道何时确切地更新了jquery,但可能是在较早的版本中done了而不是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 成功和$ .ajax的.done()方法有什么区别

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() 好的,它是jQuery 1.8 :)由于$ .ajax从jQuery 1.5返回了promise,这是一个简单的替换,以解决一致性问题(使用deferred接口):done()代替了success(),fail()表示错误()和always()代表complete()

that means if done is called, then we have to trigger success explicitly. 这意味着如果调用done ,那么我们必须显式触发success 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. 但是现在,在完成触发之前,已经调用success处理程序,然后done回调再次调用success处理程序。

Any suggestions on how to change/modify the code such that success is called only once ? 关于如何更改/修改代码的任何建议,以使success仅被调用一次?

EDIT 编辑

ajaxSettings looks like this : ajaxSettings看起来像这样:

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 ). 就像我在评论中说的那样,我认为以前您的jQuery版本低于1.5 (未实现.done() ),因此成功方法作为回调调用,然后.done()在控制台中引发错误而不会破坏任何内容(就像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 jQuery 1.4.4演示: jsfiddle

In the console : 在控制台中:

1 error : undefined is not a function (because of .done() ) 1错误undefined is not a function (由于.done()

and

1 log : Object (datas from the call) 1个日志Object (来自调用的数据)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM