Adrian P.
Adrian P.

Reputation: 5228

jQuery Ajax beforesend abort first ajax success

I have this ajax:

$.ajax({
type: "POST",
cache: false,
url: 'update_db.php',
beforeSend: function(){
    $.ajax({
    type: "POST",
    cache: false,
    async: "false",
    dataType: "json",
    url: 'check_db.php',
    success: function(data){
       var nowmod = new Date();
       var lastmod = new Date(data[0].lastmod_date);
       if(nowmod > lastmod){
        //abort here the first ajax call and
                //reload the page after 2.5 sec                                                           
                setTimeout(function(){
             location.reload();}, 2500);

       }                                            
    }
    }); 
},
success: function(data){
  alert("success");
}
}); 

I have tried to put first ajax in a JS var and call

var.abort();

Also, I have tried

return false 

In all cases the success event of first ajax call is working: alert "success"!

I have seen a case on Stackoverflow where

beforeSend : function(xhr, opts){
    if(1 == 1) //just an example
    {
        xhr.abort();
    }
},

My problem is that I have to check the DB before I abort the ajax call. Any suggestions are welcomed. Thanks.

Upvotes: 0

Views: 3894

Answers (1)

Kevin B
Kevin B

Reputation: 95048

You're overcomplicating things, just send a single ajax request to update_db.php and do the check_db.php logic inside of update_db.php.

Upvotes: 1

Related Questions