I am having the issue of my below shown lines not storing the variable into the global scope:
var somedata;
$.ajax({
cache: false,
url: verification_url,
success: function(data){
somedata = data;
}
});
alert(somedata); // Undefined
What am I doing wrong? Do I need to wrap this into a separate function or what?
The
alert()code runs before the response from$.ajaxis received.That’s why it’s
undefined.Here you can see that the alerts happen out of order. By default an AJAX request does not prevent subsequent code from running.
That’s the entire purpose of having a callback method. It is a method that gets called at the appropriate time, instead of relying on synchronous execution.