Jquery Ajax error handling to ignore aborted

后端 未结 9 748
栀梦
栀梦 2020-12-07 17:35

I want to have a global error handling method for ajax calls, this is what I have now:

$.ajaxSetup({
  error: function (XMLHttpRequest, textStatus, errorThro         


        
9条回答
  •  被撕碎了的回忆
    2020-12-07 17:52

    Because bluecollarcoders answer doesn't work for ajax requests aborted by javascript, here is my solution:

    var unloaded = false;
    ...
    $(window).bind('beforeunload', function(){
        unloaded = true;
    });
    
    
    $(document).ajaxError(function(event, request, settings) {
        if (unloaded || request.statusText == "abort") {
            return;
        }
        ...
    }
    

    e.g.

    handler = jQuery.get("foo")
    handler.abort()
    

    will now be ignored by ajaxError handler

提交回复
热议问题