我有一個奇怪的奇怪的問題中一個變量的值,這裏是我的JS代碼
function ValidateUser(username) {
var userExists = "somevalue";
$.ajax({
contentType: 'application/json, charset=utf-8',
type: "POST",
url: "/Controller/Validate",
data: JSON.stringify({ username: username }),
cache: false,
dataType: "json",
success: function (response) {
alert(response);
if (response == true) {
userExists = true;
alert("user exists: " + userExists);
} else {
alert("user exists: " + userExists);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert('error');
}
});
return userExists;
}
很好,行動正常工作和正確驗證用戶,但函數總是返回「somevalue」.....什麼給了?我的直覺是它與功能範圍有關。但我該如何解決這個問題。
好,謝謝。 – sarsnake
@sarsnake創建函數。用它作爲'成功'的價值。忍者編輯。 –