jQuery ajax generic error handling and on a case-by-case basis jQuery ajax generic error handling and on a case-by-case basis ajax ajax

jQuery ajax generic error handling and on a case-by-case basis


You can set the ajax property "global" to false in the ajax function that does it's own error handling. That should prevent global error handling. Check: http://api.jquery.com/jQuery.ajax#toptions.


I don't think you can control this with jQuery. The global ajaxError gets called on any error that happens during an ajax call. However, the "local" error callback gets called before the global callback so you could set a variable that tells the global callback not to run.

For instance:

var handledLocally = false;$('html').ajaxError(function(e, xhr, settings, exception) {    if (!handledLocally){        //run the normal error callback code and the reset handledLocally    }});error: function(){    //set handledLocally to true to let the global callback it has been taken care of    handledLocally = true;}

You can view this jsFiddle that shows how this can be accomplished (be sure to click run at the top before clicking the links): http://jsfiddle.net/e7By8/


I think set global variable is a bad idea because you can have multiple ajax request at same time. As was mentioned "local" error callback gets called before the global callback. My solution is just attach some custom property to jqXHR object for example handledLocally and set it true in your local handler and check it in global handler.

local handling:

$.ajax({   //some settings   error:function(jqXHR, textStatus, errorThrown){      //handle and after set      jqXHR.handledLocally = true   }})

global handling:

$(document).ajaxError(function (event, jqXHR, ajaxSettings, thrownError) {    if (jqXHR.handledLocally)) {        return;    }    //handle global error})