当我使用成功回调时,此解决方案可以正常工作,但是当我使用.done()时,此方法会失败,如何重试使用原始的.done().fail()和complete()注册的回调发送排队的ajax请求? /p>
When I use success callback this solution works fine, but when I use .done() this fail, how I can retry send enqueued ajax request with original .done() .fail() and complete() registered callbacks?
var requestQueue = []; $.ajaxSetup({ cache: false, beforeSend: function (jqXHR, options) { if(true){ //any condition 'true' just demonstrate requestQueue.push({request:jqXHR,options:options}); //simulate process this queue later for resend the request window.setTimeout(function(){ //this will work with success callbak option, //but with .done() the console.log("Well Done!"); // will fail $.ajax($.extend(requestQueue.pop().options, {global:false, beforeSend:null})); }, 3000) return false; } } }); $.ajax({ url:"TesteChanged.html", error: function(){ console.log("Oh nooooo!"); } }).done(function(){ console.log("Well Done!"); });我想让ajax调用入队(基于条件)以便以后重新发送,但是当重新发送它时,必须调用.done()/.fail()原始回调.使用成功"回调选项,此代码可以正常工作.
I wanna enqueue a ajax call (based in a condition) to resend later, but when a resend it, .done()/.fail() original callback must be called. With 'success' callback option this code works fine.
推荐答案我将其用于延迟AJAX请求:
I use this for delaying AJAX requests:
全局变量:
var origSend = XMLHttpRequest.prototype.send; XMLHttpRequest.prototype.send = function () { var xhr = this; var origArguments = arguments; setTimeout(function () { if (xhr.readyState === 1) { origSend.apply(xhr, origArguments); } }, 1000); };仅影响jQuery AJAX请求的变量:
Vairant that affects only jQuery AJAX requests:
$(document).ajaxSend(function (event, jqxhr, settings) { var origXhrFunc = settings.xhr; settings.xhr = function () { var xhr = origXhrFunc(); var origSend = xhr.send; xhr.send = function () { var origArguments = arguments; setTimeout(function () { if (xhr.readyState === 1) { origSend.apply(xhr, origArguments); } }, 1000); }; return xhr; }; });在jQuery解决方案中,您可以轻松地将处理程序附加到jqxhr的完成/失败/进度事件.
In jQuery solution, you can easily attach handlers to jqxhr done/fail/progress events.
更多推荐
使用完成/失败/完成回调延迟JQuery Ajax(jqXHR)请求
发布评论