I was wondering how to extract data from a returned object via jQuery’s $.get() method. IE:
function dynamicData(file){
var wantedData;
var getObj = $.get(file);
wantedData = getObj.complete(function(data){return data;});
return wantedData;
}
$(window).load(function(){
var newData = dynamicData('somefile');
alert(newData);
});
I don’t want to just stick the data to some DOM as soon as it’s gotten the new data.
I get an object alerted, but how do I get data inside of it? I have no idea how the object structure is at this point since newData is an object, but newData[0] is null. Is this by chance some sort of mapped object with key:value pairs? or are we not allowed to do it this way?
Since $.get() is asynchronous, you’re calling your alert before the get returns any data.
A better approach would be:
which will alert(data) when it becomes available.