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 ajaxError handler but still run the ajaxStart and ajaxStop handlers as usual. The jQuery ajax function documentation mentions the "global" parameter that can be set to false and passed to the $.ajax function to disable all global Ajax event handlers, but they don't mention any way to only disable some global handlers.
I can prevent the ajaxError handler by doing a test on the "url" property of the ajaxSettings object that is passed to the ajaxError function, but that seems somewhat clumsy. Does anyone here know of a way to disable the ajaxError function from running that would be clear to someone looking at the place where the $.ajax function is being called?
I can provide a simple example if anyone wants to see it.
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.
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