Why is this not working?
var names;
$.ajax({
type : 'POST',
url : postUrl+"/admin/returnUserJSON",
success : function(data){
names = data;
}
});
console.log(names);
Problem: Console.log is returning undefined.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As
$.ajaxcall is asynchronous, theconsole.logwill be called beforenames=dataline will be executed (as it’s a part of$.ajaxcallback function, which will be called only after the server’s response). Hence it will outputundefined.You can fix it two ways: either move your output into the callback function…
… or make use of Promise interface: