3

i have a backend written with php and a frontend on jQuery.. what i want to do:

  1. page contains several links with AJAX calls (each one has it's own sucess/error callbacks)
  2. php backend may return a reply something like this: {"must_relogin":true}
  3. i have to bind to some kind global AJAX event in jQuery to catch this response and do perform some my logic and to prevent any other callbacks

is it possible?

Thank you for help.

update

jQuery(document).ajaxComplete(function(a, b, c){ 
 console.log(a, b, c);
 b.abort();
 return false; });
Rafay
  • 30,950
  • 5
  • 68
  • 101

3 Answers3

2

Have you tried using event.stopImmediatePropagation()? It states "Prevents other event handlers from being called". From looking at the jQuery source we see this

if ( fireGlobals ) {
    globalEventContext.trigger( "ajaxComplete", [ jqXHR, s] );
    // Handle the global AJAX counter
    if ( !( --jQuery.active ) ) {
        jQuery.event.trigger( "ajaxStop" );
    }
}

Here is a working example of it potentially solving your problem, http://jsfiddle.net/Akkuma/95zfj/

The caveat is that it appears to not work if the ajaxCompletes are not all bound to the same element.

Akkuma
  • 2,215
  • 3
  • 16
  • 21
0

If I understand the question correctly, it is not possible to prevent the callback calls based on the response string. The problem is that the ajax global event handlers like ajaxSuccess and ajaxComplete are called after the callbacks have been executed.

Rajiv
  • 2,352
  • 1
  • 22
  • 25
0

im not sure about your scenario but here is a SO link

Abort Ajax requests using jQuery

that explains aborting the ajax calls using .abort but if a call is already made to the server then .abort cannot close an existing connection to the server, all the success call backs of the ajax calls that has been already made will be executed anyway...

HTH

Community
  • 1
  • 1
Rafay
  • 30,950
  • 5
  • 68
  • 101
  • my code: jQuery(document).ajaxComplete(function(a, b, c){ console.log(a, b, c); b.abort(); return false; }); i do not want to close any opened connections or cancel any active requests - i just need to prevent success callbacks calls – Andrew Svintsov Jul 14 '11 at 11:58
  • you **cannot** cancel the ajax request in its `success` callback if you want to cancel it you have to do it before it is being initiated – Rafay Jul 14 '11 at 12:04
  • ok.. once again :) i do not need to **cancel** ajax requests in success callbacks - i understand that this is impossible. my solution is [http://pastebin.com/fDcTgqtd] but i can not post it as an answer because of my reputation :) – Andrew Svintsov Jul 14 '11 at 12:29