Disabling some jQuery global Ajax event handlers for a request

后端 未结 4 1489
故里飘歌
故里飘歌 2020-12-02 17:01

Suppose that I have some global Ajax event handlers defined (ajaxStart, ajaxStop, and ajaxError). Usually I am fine with that, but for one request, I want to disable the aja

4条回答
  •  悲&欢浪女
    2020-12-02 17:23

    It's possible and not difficult to do.

    You just need to setup your global error handler (.ajaxError) to receive a few of the parameters that jQuery can provide to it:

    $("div.log").ajaxError(function(evt, xhr, settings) {
        if(settings.suppressErrors) {
            return;
        }
    
        // Normal processing
    });
    

    After this, you can add suppressErrors: true to the settings of any AJAX request you make, and if it fails the error handler will return without doing what it normally does.

    See it in action.

提交回复
热议问题