I have this code
var stats = {
GetMetaData : function() {
var url = 'http://www.bungie.net/api/reach/reachapijson.svc/game/metadata/'+storage.get('apikey');
$.ajax({
url: url,
success: function(data) {
return data;
}
});
return 'abc';
}
}
I call the function using stats.GetMetaData();
I would expect the value returned to be the data variable from the ajax request.
But instead it is the string ‘abc’ why is this?
How can I return the data variable?
I tried doing return $.ajax({ but that just return the function code.
Because jquery ajax requests are asynchronous by default. You can make request synchronous by using
async: falseoption or (better) use callback function.Also, as CharlesLeaf notes, using synchronous request will lock up the browser until response is received.
About the whole concept of asynchronous operations.
I would link some explanation from jquery site, but it seems to be down now.