Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling some jQuery global Ajax event handlers for a request

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.

like image 282
Elias Zamaria Avatar asked Sep 15 '11 19:09

Elias Zamaria


1 Answers

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.

like image 50
Jon Avatar answered Oct 08 '22 01:10

Jon