[英]Ajax call does not work
$('#loginbtn').click(function() {
var userName = document.getElementById('uid').value;
var password = document.getElementById('pwd').value;
$.ajax({
type: "POST",
url: "/LoginNew.aspx/Authenticate",
data: "{ 'userName': '" + userName + "' ,'password': '" + password + "' }",
async: false;
contentType: "application/json; charset=utf-8",
dataType: "json",
success: a(),
error: function(e) {
alert(e.valueOf());
}
});
alert("test");
function a() {
window.location.href = "Login.aspx";
}
});
當我接受答案時,它進入服務器端代碼,對用戶進行身份驗證,並將控制權傳遞給“ a”功能,但它不顯示login.aspx頁...有任何提示嗎?
它應該是
$('#loginbtn').click(function() {
var userName = document.getElementById('uid').value;
var password = document.getElementById('pwd').value;
$.ajax({
type : "POST",
url : "/LoginNew.aspx/Authenticate",
data : {
userName: userName ,
password: password
},
async : false, // not ; need to use ,
contentType : "application/json; charset=utf-8",
dataType : "json",
success : a, // pass the callback reference, don't invoke it
error : function(e) {
alert(e.valueOf());
}
});
alert("test");
function a() {
window.location.href = "Login.aspx";
}
});
您生成的JSON無效。 不要嘗試手動生成JSON,請使用JSON庫。
JSON.stringify({ userName: userName, password: password })
您正在調用 a()
而不是將a
分配為成功處理程序。 如果要分配函數而不是返回值,請刪除()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.