As $.ajax
call is asynchronous, the console.log
will be called before names=data
line will be executed (as it's a part of $.ajax
callback function, which will be called only after the server's response). Hence it will output undefined
.
You can fix it two ways: either move your output into the callback function...
function(data){
names = data;
console.log(names);
}
... or make use of Promise interface:
$.ajax({
type : 'POST',
url : postUrl+"/admin/returnUserJSON",
success : function(data){
names = data;
}
}).then(function() { console.log(names); });