How can I store an AJAX call's data in a variable?
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/11247066/cant-use-returned-data-in-ajax-method-of-jquery-anywhere-but-the-function-itse">Can’t use returned data in .ajax method of jQuery anywhere but the function itself</a>  </p>
</blockquote>



<p>I'm trying to store an AJAX call's return value into a variable.</p>

<pre><code>var getJSONData = function(){
    JSONData = $.getJSON('/api');
};

var reloadPage = function(json_data){
    //Do some DOM manipulation with the JSON data
    //console.log(json_data) returns undefined
};

reloadPage(getJSONData());
</code></pre>

<p>This code doesn't work. The api works though when I access it directly or using the following code</p>

<pre><code>$.getJSON('/api', function(data){console.log(data)});
</code></pre>

<p>This works and gets me the JSON data I wanted.</p>

<p>Any idea how I could make it work ?</p>
