I must be making a silly mistake but I cannot return the data I get from a $.post function and store it in a variable, not only that, I cannot return ANYTHING from within th
jQuery AJAX requests are made asynchronously, meaning that the request is started and the $.post()
method returns more or less immediately. The request is processed by the browser in the background. When the request is completed, jQuery calls the callback function you supplied to $.post()
. You can use async: false
to tell jQuery to block until the request completes, but this is not recommended as it will almost certainly result in terrible performance and a poor user experience.
What you should do instead is write your code such that the logic that depends on the AJAX response is kicked off by your callback function. Something like:
function handleAjaxResponse(responseData) {
// do something with the response.
}
function doAjax() {
$.post("demo_test_post.asp",
{
name:"Donald Duck",
city:"Duckburg"
},
function(data, status){
handleAjaxResponse(data);
});
}
doAjax();