微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用jQueryAjax“GET”调用WebMethod

我有一个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"}

所以这里是我的代码,在客户端,

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”?

解决方法

如果要使用GET调用它,则需要添加
[WebMethod]
[ScriptMethod(UseHttpGet=true)]
....

原文地址:https://www.jb51.cc/jquery/181804.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐