Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Ajax - How to stop global error handler dynamically

I have a global ajax error handler, here:

function SetAjaxErrorHandler() {
   $(document).ajaxError(function (event: JQueryEventObject, jqXHR: JQueryXHR, ajaxSettings: JQueryAjaxSettings, thrownError: string) {
       console.log("in error");
       // Call error API to log
       var url1 = appSettings["errorLogApiUrl"];
       $.ajax({
           type: "POST",
           url: url1 + "s",
           data: jqXHR.responseText,
           contentType: "application/json; charset=utf-8",
           error: (jqXhr) => {
            // I dont want the global error handler to be triggered here!
        }
    });
});

}

As you can see, on an event there is an error calling one of the other ajax calls on the page, I am trying to make another API call to log this on the server. However, if the call to log the error fails, I get caught in a recursive loop. So my question is, in my ajax call in the ajaxError() method, is there a way to stop the global error handler from being triggered?

tl;dr is there a way to use a global ajax error handler for some ajax calls but suppress it for others?

like image 215
user1373121 Avatar asked Apr 01 '14 22:04

user1373121


1 Answers

Yes, you can set global to false in the $.ajax() call:

$.ajax({
  url: 'path/to/url',
  global: false
});

From the .ajaxError() docs:

If $.ajax() or $.ajaxSetup() is called with the global option set to false, the .ajaxError() method will not fire.

like image 188
zakangelle Avatar answered Sep 18 '22 15:09

zakangelle