Possible Duplicate:
Can’t get correct return value from an jQuery Ajax call
How to return the response from an AJAX call from a function?
I have this:
get_json = (url) ->
$.getJSON "#{url}.json", {}, (json, response) ->
return json
But this compiles to:
getJson = function(url) {
return $.getJSON("" + url + ".json", {}, function(json, response) {
return json;
});
};
..and returns the response object. How can I return just the json instead?
A deferred object is being returned, use it to get your data. With your current implementation of the
get_jsonmethod, this JavaScript should work:your code can be simplified to:
There is nothing wrong with the conversion, what’s wrong is your assumption of how ajax requests work.
You can’t have a function that has a url parameter that sends off an ajax request and returns the data from the function without making the ajax request synchronous (which is a bad idea for various reasons).