0

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.

Community
  • 1
  • 1
Adrian P.
  • 5,060
  • 2
  • 46
  • 47
  • why are you doing this in beforesend rather than before even calling $.ajax for the first time? this seems very backwards and over-complicated. You can't abort an ajax request that hasn't been sent yet. – Kevin B Aug 28 '13 at 19:50
  • 1
    `update_db.php` needs to do whatever validation logic is in `check_db.php`, so you can remove the `check_db.php` ajax call... – Jason P Aug 28 '13 at 19:52
  • Why not instead just send the update request, and before actually updating the database, checking if it is there already? One less ajax request. – Kevin B Aug 28 '13 at 19:52
  • You're calling the serverside just to check if it's OK to call the serverside again? It makes no sense, you could do this with just one ajax call ! – adeneo Aug 28 '13 at 19:54
  • Unless the payload is very large and he wants to avoid sending a large amount of data just to reject it i guess... – Kevin B Aug 28 '13 at 19:56
  • @KevinB: It's about checking the last modified of a record. The logic is: concurrent users can try to change same one record in DB. check_db.php check the last mod date and time and return it. This is check against the actual time. If it's modified before do not let the second user to modify the record in DB. – Adrian P. Aug 28 '13 at 19:57
  • 2
    that can still be done using the update_db.php file alone in a single request though. – Kevin B Aug 28 '13 at 19:58
  • @KevinB: I think you're right! Focusing to solve the ajax problem I didn't see I can do it in PHP. Please answer so I can accept your answer. Thanks for suggestion. – Adrian P. Aug 28 '13 at 20:02

1 Answers1

1

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.

Kevin B
  • 94,570
  • 16
  • 163
  • 180