在Ajax Web Forms中,我能够使用ASP.NET "post“请求调用页面方法。但是我不能使用"get请求“来调用页面方法。
在这种情况下,是否可以使用Get请求来调用页面方法?您能为此提供一些建议吗?Page方法的示例代码:
[WebMethod]
public static string GetData()
{
return "test";
}发布于 2016-04-06 19:14:23
正如@vc在注释中提到的,您需要使用ScriptMethodAttribute和WebMethod,因为您希望您的请求是GET而不是POST,因此请按如下方式更改您的代码:
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string GetData()
{
return "test";
}在标记中,您可以这样做
function ShowTestMessage() {
$.ajax({
type: "GET",
url: "YourPage.aspx/GetData",
data: {},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
<input id="ButtonId" type="button" value="Show Message"
onclick = "ShowTestMessage()" />不要忘记添加以下引用
using System.Web.Script.Services;
using System.Web.Services;发布于 2016-04-06 20:04:56
Ajax GET requests to an ASP.NET Page Method?
我想这个链接会很有用。由于安全原因,ASP.Net AJAX页面方法只支持请求。
已尝试使用ScriptMethod(UseHttpGet = true)修饰该方法,但仍未命中get请求。
此外,ScriptMethod(UseHttpGet = true)的msdn文档还引用了。当此属性设置为true时,客户端代理代码使用HTTP GET来调用Web服务。不确定它是否适用于web表单中的web方法。
JQueryP.S:似乎可以与较新版本的 2.2.2一起工作。另外,请确保通过查询字符串发送数据,而不是使用POST方法中的请求正文。
https://stackoverflow.com/questions/36448982
复制相似问题