Ajax獲取數據正常,但我不知道如何從if語句返回varname,內部循環,內部函數;)。 如何從此聲明中返回var username
?謝謝。從函數返回,ajax
$.ajax({
dataType: 'json',
url: 'example.com',
type: 'POST',
success: function (data) {
for (var i = 0; i < data.users.length; i++) {
if (user_id == data.users[i].id) {
var username = data.users[i].username;
return username; // !!!How can return this
};
};
}
})
console.log(username) // error: username is not defined
阿賈克斯是異步的:http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call –