2012-05-25 25 views
18

Tengo una petición Ajax que funciona bien usando "POST", pero cuando se usa "GET" me da el siguiente error,Llamar a un WebMethod usando 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"} 

así que aquí está mi código, en el lado del cliente,

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

en el lado del servidor,

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

ninguna razón por la que estoy recibiendo de error cuando se utiliza "GET" ??

+0

¿Funciona el "post"? – dhinesh

Respuesta

58

Si desea invocarlo utilizando GET, es necesario agregar:

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

gracias, funciona. –

+0

Tenía el mismo problema. Gracias. – jkl

1

Otras maneras: puede añadir que en la configuración del archivo

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

se debe añadir el siguiente código antes de la etiqueta en el archivo web .config

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