【问题标题】:Calling a WebMethod using jQueryAjax "GET"使用 jQueryAjax "GET" 调用 WebMethod
【发布时间】:2012-05-31 19:30:26
【问题描述】:

我有一个 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”时出现错误的任何原因??

【问题讨论】:

    标签: jquery asp.net-ajax webmethod


    【解决方案1】:

    如果你想使用 GET 调用它,你需要添加:

    [WebMethod]
    [ScriptMethod(UseHttpGet=true)]
    ....
    

    【讨论】:

    • 我遇到了同样的问题。谢谢。
    【解决方案2】:

    另一种方式:你可以在配置文件中添加它

    <system.web>
        ...
        <webServices>
            <protocols>
                  <add name="HttpGet"/>
            </protocols>
        </webServices>
        ...
    </system.web>
    

    【讨论】:

    • 两个选项都可以:使用ScriptMethod(UseHttpGet=true) 进行装饰以及(区分大小写)在 webconfig 上添加 `webServices' 条目
    【解决方案3】:

    您应该在 Web .config 文件中的标记前添加以下代码。

    <location path="webservice.asmx">
      <system.web>
        <webServices>
          <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
          </protocols>
        </webServices>
      </system.web>
    </location>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-03
      • 1970-01-01
      • 2011-11-07
      • 2023-03-12
      • 1970-01-01
      • 2015-08-27
      相关资源
      最近更新 更多