The alert() code is run before receiving a response from $.ajax .
That is why it is undefined .
var somedata; $.ajax({ cache: false, url: verification_url, success: function(data){ somedata = data; alert( somedata );
Here you can see that the warnings are out of order. By default, an AJAX request does not prevent the execution of subsequent code.
This is the whole purpose of having a callback method. This is a method called at the appropriate time, rather than relying on synchronous execution.
source share