[英]ASP.NET WebMethod returns whole page to JQuery ajax requests
I am building a web application where I am calling a ASP.NET WebMethod from jQuery on click of a textbox. 我正在构建一个Web应用程序,在该应用程序中,单击文本框时,我会从jQuery调用ASP.NET WebMethod。 The problem is that it returns me the whole ASPX Page. 问题是它返回了整个ASPX页面。 How can I get just the values returned by the Web Method? 如何仅获取Web方法返回的值? This is my code: 这是我的代码:
$("#<%= 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');
}
});
});
This is my WebMethod from CodeBehind 这是我来自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;
}
The page was returning since it was not hitting the Web Method. 由于未点击Web方法,因此页面正在返回。 The below code will hit the Web Method correctly. 下面的代码将正确击中Web方法。 Pass in data as shown below. 如下所示传递数据。
$.ajax({
url: "UserGroups.aspx/GetGroupList",
data: '{ mail: "a@a.com"}',
dataType: "json",
type: "POST",
contentType: "application/json",
success: function (data) {
alert(data);
},
error: function (data) {
alert('err');
}
});
You need to pass email as parameter as webmethod is expecting a parameter. 您需要将电子邮件作为参数传递,因为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');
}
});
Also specify contentType
and dataType
同时指定contentType
和dataType
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.