Search code examples
ajaxwebmethod

WebMethods suddenly give: Invalid web service call, missing value for parameter


My ajax calls suddenly stopped working, and I cannot figure out why. What am I not seeing?

Answer: It was because ASP.NET State Server was turned off.

Here is my web method

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Test(string message)
{
    return message;
}

And here is my ajax call

$.ajax({
    url: '/Test.asmx/Test',
    data: {message:'hello'},
    type: "POST",
    async: true,
    contentType: "application/json; charset=UTF-8",
    dataType: "json",
    beforeSendMethod: function (xhr) {
        xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
    },
    success:  function (data, textStatus, jqXHR) {
        console.log('success!',data, textStatus, jqXHR);
    },
    error: function (data, textStatus, jqXHR) {
        console.log('error!',data, textStatus, jqXHR);
    }
});

Here is my error

Message:    Invalid web service call, missing value for parameter: 'message'.

StackTrace:   at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)
   at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)
   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)
   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)

ExceptionType: System.InvalidOperationException

It works if I do this:

$.ajax({
    url: '/Services/Authentication.asmx/Test',
    data: {message:'hello'},
    type: "POST",
    success:  function (data, textStatus, jqXHR) {
        console.log('success!',data, textStatus, jqXHR);
    },
    error: function (data, textStatus, jqXHR) {
        console.log('error!',data, textStatus, jqXHR);
    }
});

Except the WebMethod returns XML instead of JSON:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">hello</string>

Headers

enter image description here


Solution

  • Stupidest error ever. ASP.NET State Service was turned off, forgot to set it to start automatically when I turned it on. Restarted my computer and forgot that I was using it. Visual Studio didn't throw any exceptions about it. It just looked like I was calling the WebMethod wrong. Everything worked once I turned the service back on.