2012-05-25 14 views
18

iが「POST」を使用してうまく動作しますが、それは私に次のエラーを与える「GET」使用時にAJAX要求を持ってjQueryAjaxを使用してWebメソッドの呼び出しで、私のコードがあるので、ここで、

{"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"} 

を「GET」クライアント側、サーバー側の

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

"投稿"は機能していますか? – dhinesh

答えて

58

あなたがGETを使用して、それを起動したい場合は、あなたが追加する必要があります。

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

ありがとうございます。 –

+0

私は同じ問題を抱えていました。ありがとうございました。 – jkl

1

別の方法:あなたが設定ファイルに

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

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

<location path="webservice.asmx"> 
    <system.web> 
    <webServices> 
     <protocols> 
     <add name="HttpGet"/> 
     <add name="HttpPost"/> 
     </protocols> 
    </webServices> 
    </system.web> 
</location> 
関連する問題