2012-05-25 65 views
18

我有一個Ajax請求效果很好使用「POST」,而是用「GET」它給了我下面的錯誤時,調用使用jQueryAjax一個WebMethod「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」爲什麼我收到錯誤?

+0

「帖子」是否正常工作? – dhinesh

回答

58

如果你想調用用得到它,你需要添加:

[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>