Mr_Nizzle Mr_Nizzle - 5 months ago 92
jQuery Question

Stop $.ajax on beforeSend

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.

Answer
$.ajax({
    url : 'my_action',
    dataType: 'script',
    beforeSend : function(xhr, opts){
        if(1 == 1) //just an example
        {
            xhr.abort();
        }
    },
    complete: function(){
        console.log('DONE');
    }
});