Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for ajax success handler to finish before executing another

I have the following scenario in my javascript:

  1. Ajax loads a record.
  2. Based on the return value of that record, another ajax call grabs a html form that is added into the DOM
  3. When #2 is complete, the records that were obtained from #1 are then loaded into the form created in #2.

I am using the when so that when #2 is complete, I can load the form values from #1. The problem is, is that it appears that when doesn't wait for the success handler to finish, only the ajax call itself. I need to wait for the success function of #2 to complete (as this is what created the form in the DOM) before I can continue with loading the form with values.

If I add an alert1 at step #2, it works (which I'm guessing is because it is waiting for the alert to be clicked, and in that time, the success handler has finished.

like image 203
Lock Avatar asked Feb 25 '15 12:02

Lock


People also ask

How do I make jQuery wait for an AJAX call to finish before it returns?

If you don't want the $. ajax() function to return immediately, set the async option to false : $(". my_link").

What is difference between success and complete in AJAX?

success() only gets called if your webserver responds with a 200 OK HTTP header - basically when everything is fine. However, . complete() will always get called no matter if the ajax call was successful or not - maybe it outputted errors and returned an error - . complete() will still get called.

How do I know if AJAX request is successful?

You can check when exactly it returns "success" : // If successful, handle type chaining if ( status >= 200 && status < 300 || status === 304 ) { ... // If not modified if ( status === 304 ) { statusText = "notmodified"; ... // If we have data } else { try { ...

Is AJAX successful deprecated?

Yes, it is deprecated in jQuery 1.8 onwards. You should use . done() and use . fail() to catch the errors.


2 Answers

Do something like this where you use nested functions in the success callback:

$.ajax({
    url: "somewhere"
    success: function(data1){
        //Use data1 to determine ajax request #2
        $.ajax({
            url: "somewhere else"
            success: function(data2){
                //Do stuff with data1 and data2
             }
         });
      }
});
like image 123
winhowes Avatar answered Oct 20 '22 15:10

winhowes


You should use when or success not both. It sounds like (although code samples would make this clearer) you are attaching two separate listeners to the ajax call, but you only want one to execute after the other.
I'd either roll both into one event like:

$.ajax( ... , function(){
    // success
    // prep stuff here
    $.ajax( ... , function(){
        // second success
        // do final stuff here
    });
});

Or wrap you ajax call inside another promise (this may require a bit more reading around jQuery promises).

But if you do something like

$.when($.ajax( ... , function(){
    // thing a
}).then(function(){
    // thing b
});

Thing a and b will execute at the same time, because they are designed for pretty much the same thing.

like image 1
Ben Avatar answered Oct 20 '22 14:10

Ben