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?
                
                A: 
                
                
              
            In your callback for the first function, make your second call.
                  Daniel A. White
                   2010-01-04 23:28:00
                
              
                +4 
                A: 
                
                
              Either specify async: false in the $.ajax options, or make the second ajax call in the complete callback of the first call.
                  Stefan Kendall
                   2010-01-04 23:28:03
                
              Wouldn't that be `async: false`?
                  TM
                   2010-01-04 23:31:13
                Indeed it would. Darn that negative logic.
                  Stefan Kendall
                   2010-01-04 23:32:28
                `async: false` works great.  The order in which I call the two functions varies, so it's much preferred to the callback method.
                  Brian
                   2010-01-04 23:38:42
                
                
                A: 
                
                
              
            Use the callback parameter in the first Ajax call to call the second function.
                  sberry2A
                   2010-01-04 23:28:58
                
              
                
                A: 
                
                
              
            For best results you should probably invoke the second function in the callback of the first.
For Example:
$.post("http://www.somewebsite.com/page.php", function(data) {
  // whatever
  function2();
});
                  Nathan Taylor
                   2010-01-04 23:29:07
                
              
                
                A: 
                
                
              
            $.post("script1.php", {data:"val"}, function(response) {
  $.post("script2.php", {data:response}, function(results) {
    // this second call will be initialized when the first finishes
  });
});
                  Jonathan Sampson
                   2010-01-04 23:29:35
                
              
                
                A: 
                
                
              
            An example implementation:
function callback() {$('div#second_ajax_output').load('http://www.google.com');}
$('div#first_ajax_output').load('http://www.yahoo.com',callback);
                  Steven Xu
                   2010-01-04 23:30:41
                
              
                
                A: 
                
                
              
            Edit: Misread the question; my bad. If you ever want to have two AJAX requests executed concurrently but run a callback only after both have finished, this is how you do it!
Try this:
var completedCount = 0;
var callback = function()
{
    completedCount++;
    if (completedCount == 2)
    {
        // Do something.
    }
};
$.post('url-1', {}, callback);
$.post('url-2', {}, callback);
                  Will Vousden
                   2010-01-04 23:30:42
                
              
                
                A: 
                
                
              
            Using jQuery the simplest way is like this:
 $.ajax({
   type: "POST",
   url: "some.php",       
    success: function(msg){
       $.ajax({
         type: "POST",
         url: "some2.php",
         success: function(msg){
             alert( "End of second call" );
         }
      });
    }
 });
                  Vincent Ramdhanie
                   2010-01-04 23:30:53