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?
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 theglobaloption set tofalse, the.ajaxError()method will not fire.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With