我正在构建一个 Web 应用程序,通过单击文本框从 jQuery 调用 ASP.NET WebMethod。问题是它返回了整个 ASPX 页面。如何才能只获取 Web 方法返回的值?这是我的代码:
$("#<%= groupNameTxt.ClientID %>").click(function () {
$.ajax({
url: "../UserGroups.aspx/GetGroupList",
data: "{ }",
// dataType: "json"
type: "POST",
// contentType: "application/json",
success: function (data) {
alert(data);
},
error: function (data) {
alert('err');
}
});
});
这是我来自 CodeBehind 的 WebMethod
[System.Web.Services.WebMethod]
public static List<Groups> GetGroupList(string mail)
{
List<Groups> empList = new List<Groups>();
empList.Add(new Groups() { GroupID = 1, GroupName = "Admins" });
empList.Add(new Groups() { GroupID = 2, GroupName = "Employees" });
empList.Add(new Groups() { GroupID = 3, GroupName = "Engineers" });
empList.Add(new Groups() { GroupID = 4, GroupName = "Managers" });
empList.Add(new Groups() { GroupID = 5, GroupName = "Assistants" });
return empList;
}
最佳答案
您需要将电子邮件作为参数传递,因为 webmethod 需要参数。
$.ajax({
url: "../UserGroups.aspx/GetGroupList",
data: JSON.stringify({ email: "someemail@test.com"}),
dataType: "json"
type: "POST",
contentType: "application/json",
success: function (data) {
alert(data);
},
error: function (data) {
alert('err');
}
});
同时指定contentType
和dataType
关于javascript - ASP.NET WebMethod 将整个页面返回给 JQuery ajax 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29346288/