Is $.post asynchronous? So if I did the commands like so below would they all be done at once instead of being synchronous (1 done, wait, then the next, etc.) ?
$(document).ready(function() {
var variable1 = '1';
var variable2 = '2';
var variable3 = '3';
var variable4 = '4';
$.post("process.php", {element1: variable1}, function(data){
$("#area1").html(data);
});
$.post("process.php", {element2: variable2}, function(data){
$("#area3").html(data);
});
$.post("process.php", {element3: variable3}, function(data){
$("#area4").html(data);
});
$.post("process.php", {element4: variable4}, function(data){
$("#area4").html(data);
});
});
post() method does it's operations asynchronously. I'm setting a variable inside the call onSuccess and the calling method doesn't get a response because of this, so all my js/jquery fails on pageLoad... I would prefer if I could still keep using the $.
By default jQuery is not providing synchronous request, so we have to implicitly define synchronous request using $. ajax().
jQuery | ajax() Method. The ajax() method in jQuery is used to perform an AJAX request or asynchronous HTTP request.
jQuery post() method. The post() method is one of the commonly used HTTP methods. It is used to load a page from the server using an HTTP POST request. This method never caches the data and is generally used to send the data with the request.
The jQuery Ajax async is handling Asynchronous HTTP requests in the element. It is a procedure to send a request to the server without interruption. It is an Asynchronous method to send HTTP requests without waiting response. It is a function to working on a server without associating more than on request.
(Asynchronous Javascript and XML) The success callback function is passed the returned data, which will be an XML root element or a text string depending on the MIME type of the response. It is also passed the text status of the response. From above extract we can understand that $.post () is asynchronous.
All ajax request are asynchronos as it acronym suggest. (Asynchronous Javascript and XML) The success callback function is passed the returned data, which will be an XML root element or a text string depending on the MIME type of the response. It is also passed the text status of the response.
Ajax Post refers to sending synchronous HTTP POST requests to the webserver to load data from the server. This is a guide to JQuery Ajax Post. Here we discuss How does jQuery Ajax Post Work and Examples along with the codes and Outputs.
By default $.post()
function is asynchronous
If you want it to be synchronous Samuel J Mathew's is correct.
But if you want to keep using $.post()
instead of $.ajax()
, you should do it this way:
$.ajaxSetup({async: false});
$.post();
remember to set async back to true. Good Luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With