I have the following function:
$.ajax({
url: "../../getposts.php"
}).done(function(posts) {
var postsjson = $.parseJSON(posts);
});
How do I use the variable postsjson outside the .done() function, or how do I declare it global?
I can’t pass it to another function, because I want to use the array later on, and not when the ajax is completed.
If you just define the variable outside of the ajax call:
Then you can use it outside. Likewise, if you just leave of the
var, it will declare it globally, but that is not recommended.As pointed out by SLaks, you won’t be able to immediately use the data you get from the AJAX call, you have you wait for the
donefunction to be run before it will be initialized to anything useful.