继续中止AJAX(jqXHR)请求

时间:2013-09-16 21:04:47

标签: javascript jquery ajax prototype jqxhr

是否可以继续中止AJAX(jqXHR)请求?

类似的东西:

$(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)

这不起作用,我可以看到一个我不知道如何解决的问题:再次发送请求时没有设置任何选项。

2 个答案:

答案 0 :(得分:1)

试一试(未经测试)

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;
    }
});

答案 1 :(得分:0)

  

$.ajax(theoptions)

     

它们当前存储在ajaxOptions参数中,您必须将其存储在全局某处以便以后访问它(例如在$ .current_request中) - Kevin B