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
Solution 1:
Either specify async: false
in the $.ajax
options, or make the second ajax call in the complete
callback of the first call.
Solution 2:
$.post("script1.php", {data:"val"}, function(response) {
$.post("script2.php", {data:response}, function(results) {
// this second call will be initialized when the first finishes
});
});
Solution 3:
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" );
}
});
}
});
Solution 4:
Solution 5:
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) {
// whateverfunction2();
});
Post a Comment for "Make Sure First Ajax Function Finishes Before Second One"