18

「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」を使用するとエラーが発生する理由は何ですか?

4

3 に答える 3

69

GET を使用して呼び出す場合は、次を追加する必要があります。

[WebMethod]
[ScriptMethod(UseHttpGet=true)]
....
于 2012-05-25T10:32:28.240 に答える
2

別の方法:構成ファイルに追加できます

<system.web>
    ...
    <webServices>
        <protocols>
              <add name="HttpGet"/>
        </protocols>
    </webServices>
    ...
</system.web>
于 2013-09-19T08:00:59.710 に答える
1

Web .config ファイルのタグの前に次のコードを追加する必要があります。

<location path="webservice.asmx">
  <system.web>
    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
  </system.web>
</location>
于 2015-07-08T06:13:35.000 に答える