mkrisch
mkrisch

Reputation: 105

Proper form for JQuery Ajax call

Is it good practice to chain Ajax calls? I know ajax calls are asynchronous but I have a circumstance where I would like to trigger another ajax call based on the success or error of the first call. The code looks something like this...

$.ajax({
    url:'urlToFile.html', 
    success:function(result){
       $.ajax({
          url:'secondAjaxCall.html', 
          success:function(result){
             alert("past");
          },
          error: function(result) {
             alert("no connection");
          }
       });
    },
    error: function(result) {
       alert("no connection");
    }
}); 

Upvotes: 0

Views: 63

Answers (1)

jmar777
jmar777

Reputation: 39649

There's nothing wrong with that. If you have a lot of that going on, it might be worth looking into a control-flow library, just to keep your code free of the "pyramid effect" caused by nested asynchronous callbacks - but two levels deep or so is still pretty easy to reason about.

Upvotes: 1

Related Questions