I need to run 2 AJAX in order. The first AJAX will only be called once when the page is loaded. The second AJAX will be called after the first AJAX was called or when user clicked some buttons on the page.
I have written the following, but the first AJAX will be called each time when the second AJAX is called.
var GetFirstAJAX = function() {
var deferred = $.Deferred();
$.ajax({
...
success: deferred.resolve();
error: deferred.reject();
...
});
return deferred.promise();
};
//Second AJAX
$.when(GetFirstAJAX()).then(function()
{
$.ajax({
...
});
});