Continue an aborted AJAX (jqXHR) request

*爱你&永不变心* 提交于 2019-12-20 06:17:55

问题


Is it possible to continue an aborted AJAX (jqXHR) request?

Something like:

$(document).ajaxSend(function(event, jqXHR, ajaxOptions) {
  if(/* it enters my conditions */) {
    $.current_request = jQuery.extend(true, {}, jqXHR);
    jqXHR.abort();
  }
}

// some time in the future
$.ajax($.current_request)

This is not working, and I can see one problem that I do not know how to solve: no options are set when sending the request again.


回答1:


Give it a try (not tested)

var request_queue = [], is_processing = false;
$(document).ajaxSend(function(event, jqXHR, ajaxOptions){
    if(/* it enters my conditions */) {
        jqXHR.abort();
        request_queue.push(ajaxOptions);
    }
    else if (request_queue.length && !is_processing) {
        is_processing = true;
        while (ajaxOptions = request_queue.shift())
        {
            $.ajax(ajaxOptions);
        }
        is_processing = false;
    }
});



回答2:


$.ajax(theoptions)

They're currently stored in the ajaxOptions parameter, you'll have to store that somewhere globally to access it later (such as in $.current_request) – Kevin B



来源:https://stackoverflow.com/questions/18837293/continue-an-aborted-ajax-jqxhr-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!