jwaliszko
jwaliszko

Reputation: 17074

Terminate jquery ajax request based on beforeSend event result

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

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

function check() {
    if(success) { /* break ajax execution */ }
    else { /* continue */ }
}

Upvotes: 2

Views: 5621

Answers (3)

jwaliszko
jwaliszko

Reputation: 17074

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 */ }
}

Upvotes: 2

iDsteven
iDsteven

Reputation: 295

Just place it inside a function:

$.ajax({
    beforeSend: function() {
      check();  
    },
});

Upvotes: -1

danielQ
danielQ

Reputation: 2086

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() 

Upvotes: 0

Related Questions