tags:

views:

45

answers:

3

I have a simple Web Service method defined as:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string MyWebMethod(string foo, string bar)
{
    // DataContractJsonSerializer to deserialize foo and bar to
    //  their respective FooClass and BarClass objects.

    return "{\"Message\":\"Everything is a-ok!\"}";
}

I'll call it from the client via:

var myParams = { "foo":{"name":"Bob Smith", "age":50},"bar":{"color":"blue","size":"large","quantity":2} };

$.ajax({
    type: 'POST',
    url: 'https://mydomain.com/WebServices/TestSvc.asmx/MyWebMethod',
    data: JSON.stringify(myParams),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (response, status) {
        alert('Yay!');
    },
    error: function (xhr, err) {
        alert('Boo-urns!');
    }
});

However, this yields the following error (a breakpoint on the first line in MyWebMethod() is never hit):

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

I'd like to pass in two string parameters and use DataContractJsonSerializer to write new Foo and Bar objects. Am I missing something?

A: 

Shouldn't the signature of your service method be something like

public string MyWebMethod(Foo foo, Bar bar)

But of course, as I understand, ASMX service uses JavaScriptSerializer. You should use WCF service with webHttpBinding to use DataContractJsonSerializer.

VinayC
A: 

I know this sounds nuts but try setting your web method's response format to XML (ResponseFormat.Xml). For some reason this worked for me.

SquidScareMe
A: 

you need to formulate a "request" element in your json string, then just pass it to data element without using JSON.stringify. See code.

var myParams = "{request: \'{\"foo\":{\"name\":\"Bob Smith\", \"age\":50},\"bar\":{\"color\":\"blue\",\"size\":\"large\",\"quantity\":2}}\' }";

$.ajax({
    type: 'POST',
    url: 'https://mydomain.com/WebServices/TestSvc.asmx/MyWebMethod',
    data: myParams,
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (response, status) {
        alert('Yay!');
    },
    error: function (xhr, err) {
        alert('Boo-urns!');
    }
});
khaled