I’ve following code:
$("#get_details").click(function(){
name = $("#user_name").val();
var age,gender;
$.post("users.php", { name: name },
function(data){
if (data.status == 'success'){
age = data.message[0];
gender = data.message[1];
alert(age); //getting output
}
}, "json");
alert(age);//doesn't getting output
});
I want to access these values outside of $.post(),for this what I’ve to modify ?
First, the
vardefines them in the local scope. Withoutvarthey can be accessed fromwindow, the global object (likewindow.ageorwindow.gender).But your second alert doesn’t work because when it is called, the
$.postwouldn’t be completed yet, because it is asynchronous. You can do this instead: