webmethod 没有捕捉到 ajax POST
webmethod isn't catching ajax POST
我正在尝试使用 ajax 调用服务器端方法。这就是我得到的:
客户端:
function accept(thisButton) {
$.ajax({
type: "POST",
url: "Default.aspx/editMaxUsers",
data: '{param: "' + $(thisButton).prev().val() + '" }',
contentType: "application/json: charset=utf-8",
dataType: "json",
success: function (result) { alert("successful" + result.d); }
});
}
服务器端:
[System.Web.Services.WebMethod]
public static string editMaxUsers(string maxUsers)
{
return maxUsers; //I also have a breakpoint here that isn't stopping the execute
}
当我检查使用 firebug 的调用时,我可以看到 POST 正在发送并且看起来很好。但服务器端似乎没有发生任何事情。我做错了什么?
编辑: 不知道它是否相关,但是 url 已经包含了一些参数。我尝试了以下 url: "Default.aspx/" + window.location.search + "editMaxUsers"
但没有成功。
您的 WebMethod 的参数需要与您传入的参数名称相匹配。
变化
data: '{param: "' + $(thisButton).prev().val() + '" }',
至
data: '{maxUsers: "' + $(thisButton).prev().val() + '" }',
1.Go 到 App_Start/RouteConfig 并更改
settings.AutoRedirectMode = RedirectMode.Permanent;
至
settings.AutoRedirectMode = RedirectMode.Off;
2.As @F11 说 WebMethod 中的参数和 json 对象中的键应该同名,我强烈建议不要构建 json 对象手动。最好这样做:
function accept(thisButton) {
var data = {};
data.maxUsers = $(thisButton).prev().val();
$.ajax({
type: "POST",
url: "Default.aspx/editMaxUsers",
data: JSON.stringify(data),
contentType: "application/json: charset=utf-8",
dataType: "json",
success: function (result) { alert("successful" + result.d); }
});
}
我正在尝试使用 ajax 调用服务器端方法。这就是我得到的:
客户端:
function accept(thisButton) {
$.ajax({
type: "POST",
url: "Default.aspx/editMaxUsers",
data: '{param: "' + $(thisButton).prev().val() + '" }',
contentType: "application/json: charset=utf-8",
dataType: "json",
success: function (result) { alert("successful" + result.d); }
});
}
服务器端:
[System.Web.Services.WebMethod]
public static string editMaxUsers(string maxUsers)
{
return maxUsers; //I also have a breakpoint here that isn't stopping the execute
}
当我检查使用 firebug 的调用时,我可以看到 POST 正在发送并且看起来很好。但服务器端似乎没有发生任何事情。我做错了什么?
编辑: 不知道它是否相关,但是 url 已经包含了一些参数。我尝试了以下 url: "Default.aspx/" + window.location.search + "editMaxUsers"
但没有成功。
您的 WebMethod 的参数需要与您传入的参数名称相匹配。 变化
data: '{param: "' + $(thisButton).prev().val() + '" }',
至
data: '{maxUsers: "' + $(thisButton).prev().val() + '" }',
1.Go 到 App_Start/RouteConfig 并更改
settings.AutoRedirectMode = RedirectMode.Permanent;
至
settings.AutoRedirectMode = RedirectMode.Off;
2.As @F11 说 WebMethod 中的参数和 json 对象中的键应该同名,我强烈建议不要构建 json 对象手动。最好这样做:
function accept(thisButton) {
var data = {};
data.maxUsers = $(thisButton).prev().val();
$.ajax({
type: "POST",
url: "Default.aspx/editMaxUsers",
data: JSON.stringify(data),
contentType: "application/json: charset=utf-8",
dataType: "json",
success: function (result) { alert("successful" + result.d); }
});
}