I’m new to both ajax and jquery, so please excuse the beginner question. I’m confused about the process of calling and returning JSON objects using AJAX, and having that data being available to the rest of your program.
If I have a function like this:
function treeData(){
$.getJSON("/recipe/recipelist/", function(data) {
// sorts array by title, alphabetically
data.sort(function(a, b){
return b.title < a.title ? 1 : b.title > a.title ? -1 : 0;
});
return data;
});
}
and I try to access that data like so,
var obj = treeData();
i get an error saying that obj is undefined. Basically, I don’t understand why this isn’t working. I’m assuming it has something to do with my return statement. I’d like to be able to build another “delete” function which, when called, triggers a .getJSON request and redraws my list of database entries.
Thanks in advance.
You can’t do it that way. The
returnstatement returns from the inner function, not thetreeDatafunction.You can use
Deferredto do that.