The code:
$.post('script.php',{value:value},function(data)
{
var Aquaman= ???;
});
Now my question is if I want to set the data value into the Aquaman variable, how do I do it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Welcome to the wonderful world of asynchronous logic.
All you need to do is
var Aquaman = data;— however, that variable will ONLY exist inside that anonymous function.You can get around this by assign global variables in the usual manner:
However, this global variable won’t be set until after your AJAX call is completed. This may be desirable if, for instance,
Aquamanis being called by other AJAX calls or user events. However, the following code won’t work as you might expect:…because
console.log()is run immediately and before the$.post()is completed.The only way to prevent that is to make a synchronous AJAX call:
…but this nullifies most of the advantages of AJAX by forcing your script to stop and wait for the callback to complete. In almost all cases, it’s better to let your AJAX remain asynchronous and deal with the
datainside the callback.