POST请求失败,因为不允许GET请求
本文关键字:请求 不允许 GET 因为 失败 POST | 更新日期: 2023-09-27 18:09:27
我有一个WebMethod在我的代码后面,我通过AJAX调用。该方法在使用GET请求时有效,但我更喜欢使用POST,我也想知道为什么这不起作用和/或我做错了什么。
JavaScript $(document).ready(function () {
$.ajax({
url: "Default.aspx/HelloWorld",
method: "POST",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$("#test1").html(data.d);
},
error: function (err) {
$("#errOutput").text("ERROR: " + err.responseText);
}
});
});
c# [WebMethod]
[ScriptMethod(UseHttpGet=false)]
public static string HelloWorld()
{
return "Hello World!";
}
误差Message:
"An attempt was made to call the method 'u0027HelloWorld'u0027 using a
GET request, which is not allowed.",
StackTrace:
"at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData
methodData, HttpContext context)'r'n
at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext
context, WebServiceMethodData methodData)",
ExceptionType:"System.InvalidOperationException"
查看一些jQuery文档,我认为您使用了错误的属性。我怀疑这个:
method: "POST"
应该type: "POST"
method
对我来说也是一个更明智的名字,但是我们继续…