我的代码背后:
[WebMethod]
public bool accountExists(string username, string password) {
//code...
}
我的jquery:
$.ajax({
type: "POST",
url: "MyPage.ascx/accountExists",
data: JSON.stringify({ username: txtUsername.val(), password: txtPassword.val()}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d)
},
error: function(msg) {
alert("ERROR: " + msg.d)
}
});
我始终会在"ERROR: " + msg.d
显示警报。
MyPage.ascx位于“控件”文件夹中,因此我尝试设置url: "Controls/MyPage.ascx/accountExists"
而不做任何更改。
答案 0 :(得分:2)
ASP.NET AJAX页面方法旨在在.aspx
页面内运行而不是.ascx
用户控件。
将您的WebMethod
逻辑移至.aspx
页面并通过jQuery更新AJAX调用。