Giving the following example:
var animal= null;
$.post("ajax.php",{data: data}, function(output){
animal = output.animal;
},"json");
alert(animal);
In principle I wanted the variable to return something outside the ajax function’s success callback and I declare it outside the post. However it is still returning “null”. What am I doing wrong?
The problem is the alert command is executing before the success function, because $.post is by definition asynchronous.
To do what you want, you have to use a synchronous request (the code will not execute before the request is over) like this:
Good luck!