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, IDictionary
2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary
2 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?