Stop all active ajax requests in jQuery

前端 未结 16 1053
自闭症患者
自闭症患者 2020-11-22 13:51

I have a problem, when submitting a form all active ajax request fail, and that triggers error event.

How to stop all active ajax requests in jQuery without trigerri

16条回答
  •  眼角桃花
    2020-11-22 14:35

    I had some problems with andy's code, but it gave me some great ideas. First problem was that we should pop off any jqXHR objects that successfully complete. I also had to modify the abortAll function. Here is my final working code:

    $.xhrPool = [];
    $.xhrPool.abortAll = function() {
                $(this).each(function(idx, jqXHR) {
                            jqXHR.abort();
                            });
    };
    $.ajaxSetup({
        beforeSend: function(jqXHR) {
                $.xhrPool.push(jqXHR);
                }
    });
    $(document).ajaxComplete(function() {
                $.xhrPool.pop();
                });
    

    I didn't like the ajaxComplete() way of doing things. No matter how I tried to configure .ajaxSetup it did not work.

提交回复
热议问题