2

My WebMethod looks like this:

[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json, UseHttpGet=true)]
public List<Person> HelloWorld(string hello)
{
   List<Person> persons = new List<Person> 
   {
      new Person("Sarfaraz", DateTime.Now),
      new Person("Nawaz", DateTime.Now),
      new Person("Manas", DateTime.Now)
   };
   return persons;
}

And I'm trying to call this method using jQuery as:

var params={hello:"sarfaraz"}; //params to be passed to the WebMethod
$.ajax
({
    type: "GET",   //have to use GET method
    cache: false,
    data: JSON.stringify(params),
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    url: "http://localhost:51519/CommentProviderService.asmx/HelloWorld",
    processData: true,
    success: onSuccess,
    error: onError      //it gets called!
});

But it doesn't work. Instead of calling onSuccess callback, it calls onError in which I use alert as:

alert(response.status + " | " + response.statusText + " | " + response.responseText + " | " + response.responseXML );

which prints this:

500 | Internal Server Error | {"Message":"Invalid web service call, missing value for parameter: \u0027hello\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"} | undefined

I don't understand why I get this error.

If I change the jQuery call to use POST method and make UseHttpGet=false , then it works great. But I want it to work with GET. What needs to be fixed?

Nawaz
  • 353,942
  • 115
  • 666
  • 851

2 Answers2

1

HTTP GET expects the parameters to all be encoded in the URL.

The problem is that you are doing a JSON.stringify on your payload. jQuery.ajax is really just looking for simple dictionary that it can turn into a series of Query String params.

So if you have an object like this:

{ name: "value" }

jQuery will append it to the end of your URL like this:

?name=value

Use Firebug, Chrome Developer Tools, or IE Developer tools to inspect the outgoing URL and I suspect you will see it is in a format that ASP.Net cannot translate.

Josh
  • 44,706
  • 7
  • 102
  • 124
  • Now, I tried with `{ hello : "sarfaraz" }`, and I get this error : `500 | Internal Server Error | {"Message":"Invalid JSON primitive: sarfaraz.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()....` – Nawaz Sep 29 '12 at 14:13
  • @Nawaz - Try changing your data param to look like this: `data: {"hello":"sarfaraz"}` – Josh Sep 29 '12 at 14:18
  • Ok, it's been a little while since I did this with ASP.Net but I vaguely remember having to completely enclose my data in quotes. So you have one of two things you can try. 1.) `data: "{'hello':'sarfaraz'}"` 2.) stick the data in the URL as query string parameters and use `jQuery.get()` instead. – Josh Sep 29 '12 at 14:24
0

To make an $.ajax request in GET, you will have to set your data as params="hello=sarfaraz"

So the complete code snippet can simply be

var params="hello=sarfaraz"; //params to be passed to the WebMethod
$.ajax
({
    type: "GET",   //have to use GET method
    cache: false,
    data: params,
    dataType: 'json',
    url: "http://localhost:51519/CommentProviderService.asmx/HelloWorld",
    success: onSuccess,
    error: onError    //it gets called!
});

Hope that helps!

vbn
  • 789
  • 1
  • 6
  • 16