gpt4 book ai didi

c# - 为什么我的 Ajax 不工作?

转载 作者:太空狗 更新时间:2023-10-30 01:04:32 25 4
gpt4 key购买 nike

我试图从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com