strange behavior with javascript global variable and jQuery
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/5316697/jquery-return-data-after-ajax-call-success">jQuery: Return data after ajax call success</a>  </p>
</blockquote>



<p>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.</p>

<pre><code>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
};
</code></pre>

<p>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.</p>

<p>Is that because of the jQuery method? I have no idea...</p>

<p>It is also the same behavior if I just put subtree = "whatever". It becomes undefined as soon as I exit the function..</p>
