2

Is there any way to terminate ajax execution, based on beforeSend result ?

$.ajaxSetup({
    beforeSend: check,
});

function check() {
    if(success) { /* break ajax execution */ }
    else { /* continue */ }
}
jwaliszko
  • 16,942
  • 22
  • 92
  • 158
  • 1
    possible duplicate of [Stop $.ajax on beforeSend](http://stackoverflow.com/questions/10507079/stop-ajax-on-beforesend) Out of votes – Esailija Jun 18 '12 at 20:43

3 Answers3

2

I see that answer can be found here: Stop $.ajax on beforeSend. It should look like below:

$.ajaxSetup({
    beforeSend: check,
});

function check(xhr) {
    if(success) { xhr.abort(); }
    else { /* continue */ }
}
Community
  • 1
  • 1
jwaliszko
  • 16,942
  • 22
  • 92
  • 158
0

You can try with the abort() method:

var xhr = $.ajax({     
    type: "POST",     
    url: "test",     
    data: "thedata",     
    success: function(msg){        alert( "Back again: " + msg );     } });  

//kill the request... try these on beforeSend
xhr.abort() 
danielQ
  • 2,016
  • 1
  • 15
  • 19
-1

Just place it inside a function:

$.ajax({
    beforeSend: function() {
      check();  
    },
});
iDsteven
  • 295
  • 2
  • 8