Liron Harel
Liron Harel

Reputation: 11247

Internal Server Error when sending JSON object to .NET webservice

I have a problem submitting a complex JSON object to a .NET web service.

The Ajax JavaScript code:

$.ajax({
    type: "POST",
    url: WEBSERVICE_ADMINISTRATIO_URL + "RecieveData",

    data: JSON.stringify({data: _data}), // _data is the Javascipt object {}
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        alert(data);
    },
    failure: function (errMsg) {
        alert(errMsg);
    },
    error: function (jqXHR, textStatus, errorThrow) {
        alert(textStatus);
    }
});

The .NET webservice

 [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string RecieveData(string data)
    {

        return "";
    }

I get an Internal Server Error 500 when trying to submit the data to the webservice from the client.

I have other webservice function that just return data and they work fine. I gave both GET and POST permission in the web.config.

Here's the detailed error:

"{"Message":"No parameterless constructor defined for type of 
 \u0027System.String\u0027.","StackTrace":"   at 
system.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n   at 
System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n   at 
System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)\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.MissingMethodException"}"

Thanks.

Upvotes: 1

Views: 3206

Answers (1)

Liron Harel
Liron Harel

Reputation: 11247

Thank you all for your support. I was able to solve this by changing the parameter data type from string to Object.

 public string RecieveData(Object data)
{

    return "";
}

Upvotes: 2

Related Questions