我试图从 Ajax 代码调用方法,但我的 ajax 代码根本无法进入该方法。 我的代码可能有什么问题?
C# 方法:
[WebMethod]
public static bool UserNameExists(string sendData)
{
bool a;
a = DataCheck.CheckDBUser(sendData);
return a;
}
Ajax :
$('#Button2').click(function () {
var name = document.getElementById('<%= UserTxt.ClientID %>').value;
$.ajax({
type: 'POST',
url: 'Register.aspx/UserNameExists',
data: '{ }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(msg) {
if (msg.d == true) {
$("#UserLb").text("User Name is OK!");
} else {
$("#UserLb").text("User Name NOT avliable!");
}
}
});
});
注意:当我使用 alert();命令只是为了检查它是否正在运行 - 没问题。
谢谢。
最佳答案
您的方法需要一个您没有在 ajax 调用中传递的参数。 这样做:
$('#Button2').click(function () {
var name = document.getElementById('<%= UserTxt.ClientID %>').value;
$.ajax({
type: 'POST',
url: 'Register.aspx/UserNameExists',
data: {sendData:name },
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(msg) {
if (msg.d == true) {
$("#UserLb").text("User Name is OK!");
} else {
$("#UserLb").text("User Name NOT avliable!");
}
}
});
});
并从 data:'{}'
中删除单引号,它应该是 data: {}
关于c# - 为什么我的 Ajax 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22882299/