使用完成/失败/完成回调延迟JQuery Ajax(jqXHR)请求

编程入门 行业动态 更新时间:2024-10-24 13:25:03
本文介绍了使用完成/失败/完成回调延迟JQuery Ajax(jqXHR)请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我使用成功回调时,此解决方案可以正常工作,但是当我使用.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)请求

本文发布于:2023-11-25 04:09:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628265.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   JQuery   jqXHR   Ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!