I am trying to run two ajax requests one after the other. So, I could use the ajax success() function:
$.ajax({
...
success: function() {
//here the second ajax request
}
});
The problem is, that the first ajax request gets only executed, when an condition is true. So my code looks like:
if($cond) {
$.ajax({
...
});
}
$.ajax({
...
});
How can I run these request ony by one? Or is it standard that the second gets only executed when the first has finished?
jQuery 1.5 introduced deferred objects [docs], and
$.ajax[docs] is returning one:Reference:
deferred.then[docs]Update:
In your case you can do this: