I have this jQuery ajax call:
$.ajax({ url : 'my_action', dataType: 'script', beforeSend : function(){ if(1 == 1) //just an example { return false } }, complete: function(){ console.log('DONE'); } });
I want to stop the ajax call under the beforeSend
if the condition returns true
but return false does not stop the ajax call.
How can I stop
the ajax call on the beforeSend
?
======= UPDATE =========
return false
works as well.
We may use the abort() method to cancel a sent AJAX request.
ajax has async property. If you set it to false it will block. possible duplicate of Synchronous calls with jquery -- you cannot block the runtime without blocking the browser though.
Cut and paste whatever code you need to execute in the callback function passed to success . Some good answer is already provided.
$.ajax({ url : 'my_action', dataType: 'script', beforeSend : function(xhr, opts){ if(1 == 1) //just an example { xhr.abort(); } }, complete: function(){ console.log('DONE'); } });
Most of the jQuery Ajax methods return an XMLHttpRequest (or the equivalent) object, so you can just use abort().
var test = $.ajax({ url : 'my_action', dataType: 'script', beforeSend : function(){ if(1 == 1) //just an example { test.abort(); return false } }, complete: function(){ console.log('DONE'); } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With