Stop all active ajax requests in jQuery

前端 未结 16 920
自闭症患者
自闭症患者 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:32

    Here's how to hook this up on any click (useful if your page is placing many AJAX calls and you're trying to navigate away).

    $ ->
        $.xhrPool = [];
    
    $(document).ajaxSend (e, jqXHR, options) ->
        $.xhrPool.push(jqXHR)
    
    $(document).ajaxComplete (e, jqXHR, options) ->
        $.xhrPool = $.grep($.xhrPool, (x) -> return x != jqXHR);
    
    $(document).delegate 'a', 'click', ->
        while (request = $.xhrPool.pop())
          request.abort()
    

提交回复
热议问题