0

I want to set up global behavior for all AJAX requests, and sometimes I need to interrupt queue like this

$.ajaxSetup({
    success: function(data, textStatus, jqXHR) {
        if ( data.error ) { jqXHR.reject(); }
    }
});
// later
$.ajax({ some: option }).done( function(html) { do_something(html); } );

here I want to finish (do not call done with function(html)) when data.error is present

Is it possible ? Or maybe exists another way for it ?

bor
  • 181
  • 1
  • 6
  • I'm not sure I follow... :( You simply need to use `error: function(){}` instead of `done`. isn't it? – gdoron May 28 '12 at 08:24

1 Answers1

2

You can't resolve the jqXHR directly.

The jqXHR implements the Promise interface, which can be used to get the state or add callbacks but, can't be used to change the state.

Reference http://api.jquery.com/jQuery.ajax/#jqXHR

Did you try $.ajaxSetup(success: defaultHandler})? But this will work only if you provide a handler in the original options. like : $.ajax({url: 'someurl' , success: someHandler});

For more ideas , see if this helps: http://forum.jquery.com/topic/stop-a-deferred-callback-queue-in-progress

sbr
  • 4,735
  • 5
  • 43
  • 49