I have a url that returns some text. I would assign this string to the region variable. How can I do it?
var region="";
$.get("retrieve/region", function(data) {
region=data;
alert(data);
});
alert("r= "+region);
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.
The function defined inside the
$.get()call won’t run right away, so youralertprints the value ofregionbefore the$.get()callback has assigned it a value. If you put thealertinside of the function, so that it ran after the value was assigned, you would see that your code does indeed work (jsfiddle).