Jquery – WCF, POSTing JSONized data

jqueryjsonnetpostwcf

I have a complex type:

[DataContract]
public class CustomClass
{
   [DataMember]
   public string Foo { get; set; }
   [DataMember]
   public int Bar { get; set; }
}

I then have a WCF RESTful webservice that has this in it:

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/class/save")]
bool Save(CustomClass custom);

so on the browser side I jsonized my CustomClass object to it looks like:

var myClass = "{ foo: \"hello\", bar: 2 }";
$.ajax({
    contentType: "application/json",
    data: { custom: myClass },
    dataType: "json",
    success: callback,
    type: "POST",
    url: "MyService.svc/class/save"
});

I submit the data w/ jquery using $.ajax so I can manually set the content type to "application/json" and when it submits, the postbody looks like

custom=<uri encoded version of myClass>

I get the following error:

The server encountered an error processing the request. The exception message is 'There
was an error checking start element of object of type MyAssembly.CustomClass. Encountered unexpected
character 'c'.'. See server logs for more details. The exception stack trace is:
at System.Runtime.Serialization.XmlObjectSerializer.IsStartObjectHandleExceptions
(XmlReaderDelegator reader)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.IsStartObject(XmlDictionaryReader
reader)
at System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.ReadObject(Message message)
at System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.DeserializeRequest(Message message
, Object[] parameters)
at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message
message, Object[] parameters)
at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message
, Object[] parameters)
at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object
[] parameters)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

I've tried wrapping my json'ized data…i've tried using $.post to send the message (but that doesnt set the contenttype to application/json so the webservice doesn't understand)..any ideas?

Best Answer

The problem that you've escaped object correctly, but when you're building complex Json object in jQuery post method you are not escaping wrapper. So you need either escape whole JS object like this: "{ \"custom\": \"{ foo: \"hello\", bar: 2 }\"}" (I'm actually didn't try this myself, but should work), or (probably better solution) use JSON.stringify({ custom: myClass })

WCF is really sensitive toward JSON objects that it receives for serialization.

Related Topic