Possible Duplicate:
jQuery: Return data after ajax call success
first of all I’m not very experienced with JS or jQuery, but as far as my programming skills go I can’t seem to find anything that’s wrong with my approach.
var subtree;
$.post("addTreeServlet",
{
term:clickedNode,
max:maxId,
id:clickedId
},
function(jsonResponse){
subtree = eval('(' + jsonResponse + ')');
}
);
$jit.json.prune(subtree, level);
return {
'id': nodeId,
'children': subtree.children
};
I went through the code with the debugger, and here is what happens: The program comes back from the addTreeServlet and transmits a String representing JSON data. the eval() function takes this String and transforms it into an JSON Object (again, it looks perfectly fine in the debugger). But as soon as I leave the function(jsonResponse) the var subtree is shown as undefined, and I can’t figure out why.
Is that because of the jQuery method? I have no idea…
It is also the same behavior if I just put subtree = “whatever”. It becomes undefined as soon as I exit the function..
It’s waiting for post response, but your fallowing script immediately parsed by browser. So you need to do what you want to do with ajax response after post response received.