4

I have a global error handler for AJAX errors:

$(document).ready(function() {
    $(document).ajaxError(ajaxErrorHandler);
});

function ajaxErrorHandler(event, jqxhr, settings, exception) {

    // ...

    // How to stop further processing of the AJAX response here?
}

Once I processed an error, I do not want the AJAX response to be processed any further. How can I stop the execution of the complete callback? (My AJAX requests are made inside a library and I cannot define their parameters).

UPDATE:

Calling jqxdr.abort() does not do the job.

Alexey
  • 2,542
  • 4
  • 31
  • 53
  • possible duplicate of [Stop all active ajax requests in jQuery](http://stackoverflow.com/questions/1802936/stop-all-active-ajax-requests-in-jquery) – Roko C. Buljan Feb 27 '14 at 03:25
  • I don't think you can because the complete handler will get called before the ajaxError handler is called.. see http://jsfiddle.net/arunpjohny/mM87r/1/ – Arun P Johny Feb 27 '14 at 03:28
  • @Roko I do not think this question is a duplicate. At least, the problem is significantly different. – Alexey Feb 27 '14 at 03:54
  • @Arun From [here](http://api.jquery.com/jQuery.ajax/): `complete` - _a function to be called when the request finishes (after `success` and `error` callbacks are executed)._ I am absolutely sure that some event handlers get executed after `ajaxError` in my code. – Alexey Feb 27 '14 at 04:01
  • complete gets called after success and error callbacks not after the events – Arun P Johny Feb 27 '14 at 04:08
  • @Arun I would like to stop any further processing of the AJAX responce, if it is possible. – Alexey Feb 27 '14 at 04:15

2 Answers2

2

You can try this in your handler:

function ajaxErrorHandler(event, jqxhr, settings, exception) {

    // ...

    // How to stop further processing of the AJAX response here?
    event.stopPropagation();
}

It will stop event propogation to the parent handlers.

Azfar Niaz
  • 1,516
  • 4
  • 13
  • 21
  • Thanks for your response. It does not work for me. There is code that gets executed after `event.stopPropagation()`. I do not know what callback/event this code is hooked to. – Alexey Feb 27 '14 at 04:41
  • It might be possible that another event handler is attached to ajaxComplete event. You may use $.ajaxSetup() and providing it with setting object that should be declaring global to false and then register your handler. – Azfar Niaz Feb 27 '14 at 05:37
  • I think the code is attached to the local `complete` callback. The one that is defined in the `jQuery.ajax()` parameters. – Alexey Feb 27 '14 at 05:47
  • Maybe the code that not supposed to be executed gets called before your event? – UnTraDe Feb 27 '14 at 06:33
  • @UnTraDe It is executed after the `ajaxError`. I test it in a debugger. – Alexey Feb 27 '14 at 07:05
  • @AzfarNiaz: when an ajax call, 4 separate events are triggered (in the follwing order) : local error, global error, local complete, global complete. Your solution will stop the propagation of the global error event, but there is no way to prevent the triggering of the following next 2 events. – LeGEC Mar 03 '14 at 08:35
1

Here is the full list of ajax events being triggered : jQuery doc

The purpose of the complete and ajaxComplete events is to be called regardless of the request state (success or error). If its execution depends on some condition set in the ajaxErrorHandler, I would argue it shouldn't be bound to complete anymore.

You can plug your current complete callback once on success and once on error, so that calling evt.stopPropagation() and evt.stopImmediatePropagation() in the global event handler will prevent the callback execution.


The jQuery code which handles the events succession on succes/failure is here.

As you can see, it involves 2 distinct deferred objects (one which handles the resolved / rejected part, one which handles the completed part), and two separate event triggers.

There is no built in way to cleanly interrupt this flow : the handling of the global ajaxError event is completely separate from the following complete and ajaxComplete events.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • Thanks for the response. My AJAX requests are made by a JavaScript library and I have limited control over them. But even if I had a full control over the code, I would still wanted to process some most critical errors in the global `ajaxError` (I redirect to an error page there) without warring that something else could be executed before the page gets refreshed. – Alexey Feb 28 '14 at 00:19
  • How do you "redirect to an error page" ? If you use `window.location.assign(url)`, it will stop all javascript execution on your current page. – LeGEC Feb 28 '14 at 14:17
  • I have just tried it. It does not stop JavaScript. Even lines immediately following `window.location.assign(url)` are executed. If I found a way to stop all JavaScript on the page it would probably solve my problem. – Alexey Mar 01 '14 at 01:50