silaunosti silaunosti - 7 months ago 9
Javascript Question

Global ajaxError event before local error event

I have a global ajaxError event, something like this:

$(document).ajaxError(function myErrorHandler(event, xhr, ajaxOptions, thrownError) {
alert("There was a global ajax error!");
});


And I have many local ajaxEvents, something like this:

$(imageUploadForm).ajaxForm({
url: assetsUplUrl,
type: 'POST',
dataType: 'json',
data: {project_id: projectId, type: 'image', widget: widget},
error: function(responseText, status, xhr, form){
alert("There was a local ajax error!");
}
});


How can i get the global error event to trigger before the local one without changing in the local ajax?

Answer

I found the solution. I change

$(document).ajaxError(function myErrorHandler(event, xhr, ajaxOptions, thrownError) {
    alert("There was a global ajax error!");
});

to

 $.ajaxSetup({
        error : function(jqXHR, textStatus, errorThrown) {
            alert("There was a global ajax error!");           
            }
    });

and global handler calling before local.