Disabling some jQuery global Ajax event handlers for a request

后端 未结 4 1487
故里飘歌
故里飘歌 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:45

    When you look at this if statement from the jquery/ajax source https://github.com/jquery/jquery/blob/master/src/ajax.js#L579-582 , which is one of many of the same kind, it is clear that your problem can not be solved by a jquery parameter alone.

    My suggestion would be to:

    • set global to false, like Vaibhav Gupta said

    • map your global handlers to local in the specific ajax call and trigger the global event through the $.event.trigger method

    Sample:

    $.ajax({
        url: "test.html",
        global: false,
        beforeSend: function(){$.event.trigger('ajaxStart');},
        complete: function(){$.event.trigger('ajaxStop');},
        error: your_error_handler
    });
    

提交回复
热议问题