I have the usual callback method when doing AJAX calls. However, I have a situation where I need to store the success data from the callback to a variable, but when I try and retrieve the data it comes up null. Can anyone share some light into where I'm going wrong? Thanks in advance!
function getData(url, callback){
$.ajax({
type: 'GET',
url: url,
success: callback
});
};
var storeThedata = (function(){
function theData(){
var newdata = null;
getData('someurl.json', function(data){
newdata = data; // newdata now isn't null woo!
});
return newdata;
}
return {
getTheData: theData
}
})();
var stored = storeThedata.getTheData();
console.log(stored); // Shouldn't be null