Reputation: 40633
I need to call a particular PHP script on my server several times. As each call will take some time (e.g. about .5 seconds) and each call is independent of each other, I want to call the scripts concurrently. Right now, I have something like this:
$(document).ready(function() {
$.ajax({
url: 'main.php',
method: 'POST',
data: { foo: 'foo' },
success: function(result) {
console.log(result);
}
});
$.ajax({
url: 'main.php',
method: 'POST',
data: { bar: 'bar' },
success: function(result) {
console.log(result);
}
});
});
Instead of making these calls sequentially, I want to do them concurrently. How do I do that?
Upvotes: 0
Views: 1049
Reputation: 20737
Not sure what issue you are experiencing but you are making concurrent AJAX calls because jQuery defaults to async: true
. If it defaulted to false
then it would be called SJAX lol
One issue which you might be experiencing would be a session handling lockup.
If main.php
is using session_start()
then it will simply queue the bar request until foo finishes. Or you can manually call session_write_close()
to avoid the lockup.
Upvotes: 1
Reputation: 2425
Ajax requests are asynchronous by nature. Based on your example, each of the calls will be dispatched without waiting for the server's response
Upvotes: 0
Reputation: 391
Set your async = true on your AJAX calls to make them asynchronous.
$.ajax({
async: "true",
url: 'main.php',
method: 'POST',
data: { foo: 'foo' },
success: function(result) {
console.log(result);
}
});
$.ajax({
async: "true",
url: 'main.php',
method: 'POST',
data: { bar: 'bar' },
success: function(result) {
console.log(result);
}
});
Upvotes: 0