views:

450

answers:

2

I can't seem to call a web service method from Ajax with both POST and GET.

Initially only the POST would work and GET would causes this error:

{"Message":"An attempt was made to call the method \u0027getData\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"}

I fixed that by adding this attribute: [ScriptMethod(UseHttpGet=true)] but now GET causes this error:

{"Message":"An attempt was made to call the method \u0027getData\u0027 using a POST 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"}

So is it true that you can only use either POST or GET and not both from Ajax? Does anyone know why this occurs or if there is a workaround?

Thanks in advance!

A: 

You can configure an ASMX service to respond to both GET and POST, but I don't believe there's any reasonable way to make them respond to GETs with JSON. Without the JSON serialization, they aren't really appropriate for use in AJAX calls.

If you want to request JSON via GET, you'll need to use an HttpHandler or WCF service.

Also, you should be sure that you know what you're doing before exposing JSON via GET.

Dave Ward
I understand the risk but the GET requests are meant for use with JSONP.
celticpride
A: 

You should try this with WCF. ASMX web services are now considered to be "legacy technology", and Microsoft has said they are now in "maintenance mode", and are unlikely to have bugs fixed.

John Saunders