2012-05-25 16 views
18

Ho una richiesta Ajax che funziona bene con "POST", ma quando viene utilizzato "GET" mi dà il seguente errore,Chiamare un WebMethod utilizzando jQueryAjax "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"} 

ecco il mio codice, su Dal lato client,

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"); } 
     }); 
    } 

sul lato server,

[WebMethod] 
public static string GetSomething() 
{ 
    return "got something"; 
} 

alcuna ragione per cui sto ottenendo l'errore quando viene utilizzato "GET" ??

+0

Il "post" funziona? – dhinesh

risposta

58

Se si vuole invocare utilizzando GET, è necessario aggiungere:

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

grazie che funziona. –

+0

Stavo soffrendo dello stesso problema. Grazie. – jkl

1

un altro modo: è possibile aggiungere in config file

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

si dovrebbe aggiungere il seguente codice prima del tag nel file .config Web.

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