Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make sure first ajax function finishes before second one

I have a JavaScript function that makes two consecutive Ajax requests using jQuery. I want to make sure that the first request has loaded before the second function is called. Is there a way I can do this?

like image 663
Brian Avatar asked Jan 04 '10 23:01

Brian


2 Answers

Either specify async: false in the $.ajax options, or make the second ajax call in the complete callback of the first call.

like image 168
Stefan Kendall Avatar answered Sep 25 '22 10:09

Stefan Kendall


$.post("script1.php", {data:"val"}, function(response) {
  $.post("script2.php", {data:response}, function(results) {
    // this second call will be initialized when the first finishes
  });
});
like image 32
Sampson Avatar answered Sep 23 '22 10:09

Sampson