我有一个使用“POST”的ajax请求,但是当使用“GET”时它会给我以下错误,
{"Message":"An attempt was made to call the method \u0027GetSomething\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"}
所以这是我的代码,在客户端,
function test() {
$.ajax({
url: "Default4.aspx/GetSomething",
type: "GET",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (res) { debugger; alert(res.d); },
error: function (res) { debugger; alert("error"); }
});
}
在服务器端,
[WebMethod]
public static string GetSomething()
{
return "got something";
}
使用“GET”时出现错误的任何原因??
答案 0 :(得分:66)
如果要使用GET调用它,则需要添加:
[WebMethod]
[ScriptMethod(UseHttpGet=true)]
....
答案 1 :(得分:2)
另一种方法:您可以在配置文件中添加它
<system.web>
...
<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
</webServices>
...
</system.web>
答案 2 :(得分:1)
您应该在Web .config文件中的标记之前添加以下代码。
<location path="webservice.asmx">
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
</location>