jQuery 使用 jQueryAjax“GET”调用 WebMethod
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10750631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling a WebMethod using jQueryAjax "GET"
提问by Vishal Gowda
i have a ajax request which works well using "POST" but when used "GET" it gives me the following error,
我有一个 ajax 请求,它使用“POST”运行良好,但是当使用“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"}
so here is my code, on the client side,
所以这是我的代码,在客户端,
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"); }
});
}
on the server side,
在服务器端,
[WebMethod]
public static string GetSomething()
{
return "got something";
}
any reason why i am getting error when used "GET" ??
使用“GET”时出现错误的任何原因?
回答by Icarus
If you want to invoke it using GET, you need to add:
如果要使用 GET 调用它,则需要添加:
[WebMethod]
[ScriptMethod(UseHttpGet=true)]
....
回答by Nishant Kumar
Another Ways: You can add it in config File
另一种方法:您可以将其添加到配置文件中
<system.web>
...
<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
</webServices>
...
</system.web>
回答by Tabish
you should add the following code before the tag in Web .config file.
您应该在 Web .config 文件中的标记之前添加以下代码。
<location path="webservice.asmx">
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
</location>