I have a javascript function that requests some ajax data and returns a JSON object. Then it must return the object.
The problem is that I don't know how to return a function from an Ajax callback. Of course,
myFunction: function() { $.get(myUrl, function(data) { return data; }); }
does not work because the internal function is returned instead of the external.
On the other hand, doing what I need only inside the callback will violate my MVC unit: this code is in the model, and I wanted to use the result object in the controller.
Workaround Workaround
myFunction: function() { var result = $.ajax({ url: myUrl, async: true, dataType: 'text' }).responseText; return eval(result); }
which has the disadvantage of blocking the browser, waiting for a response (and using eval, which I would prefer to avoid).
Are there any other solutions?
jquery asynchronous callback return
Andrea
source share