title: WCF, POSTing JSONized data
body: <p>I have a complex type:</p>
  
  <pre><code>[DataContract]
  public class CustomClass
  {
     [DataMember]
     public string Foo { get; set; }
     [DataMember]
     public int Bar { get; set; }
  }
  </code></pre>
  
  <p>I then have a WCF RESTful webservice that has this in it:</p>
  
  <pre><code>[OperationContract]
  [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/class/save")]
  bool Save(CustomClass custom);
  </code></pre>
  
  <p>so on the browser side I jsonized my CustomClass object to it looks like:</p>
  
  <pre><code>var myClass = "{ foo: \"hello\", bar: 2 }";
  $.ajax({
      contentType: "application/json",
      data: { custom: myClass },
      dataType: "json",
      success: callback,
      type: "POST",
      url: "MyService.svc/class/save"
  });
  </code></pre>
  
  <p>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</p>
  
  <pre><code>custom=&lt;uri encoded version of myClass&gt;
  </code></pre>
  
  <p>I get the following error:</p>
  
  <p>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&amp; rpc)
     at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; rpc)
     at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p>
  
  <p>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?</p>