title: Consuming WCF from jQuery as JSON
body: <p>With a contract:</p>
  
  <pre><code>namespace ACME.FooServices
  {
      [ServiceContract]
      public interface IFooService
      {
          [OperationContract]
          [WebInvoke(Method = "POST",
                     ResponseFormat = WebMessageFormat.Json,
                     RequestFormat = WebMessageFormat.Json,
                     BodyStyle = WebMessageBodyStyle.Bare)]        
          FooMessageType Foo(string name);
      }
  
      [DataContract]
      public class FooMessageType
      {
          string _name;
          string _date;
  
          [DataMember]
          public string Name
          {
              get { return _name; }
              set { _name = value; }
          }
  
          [DataMember]
          public string Date
          {
              get { return _date; }
              set { _date = value; }
          }
      }
  }
  </code></pre>
  
  <p>And implementation:</p>
  
  <pre><code>using System;
  using System.ServiceModel.Activation;
  
  namespace ACME.FooServices
  {
      [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
      public class FooService : IFooService
      {
          public FooMessageType Foo(string name)
          {
              string l_name = (String.IsNullOrWhiteSpace(name)) ? "Anonymous" : name;
  
              return new FooMessageType {Name = l_name, Date = DateTime.Now.ToString("MM-dd-yyyy h:mm:ss tt")};
          }
      }
  }
  </code></pre>
  
  <p>Configured in the web.config as:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
      &lt;services&gt;
          &lt;service name="ACME.FooServices.FooService"&gt;
              &lt;endpoint address="" behaviorConfiguration="ACME.FooBehaviour" binding="webHttpBinding" contract="ACME.FooServices.IFooService" /&gt;
          &lt;/service&gt;
      &lt;/services&gt;
      &lt;behaviors&gt;
          &lt;endpointBehaviors&gt;
              &lt;behavior name="ACME.FooBehaviour"&gt;
                  &lt;webHttp /&gt;
              &lt;/behavior&gt;
          &lt;/endpointBehaviors&gt;
          &lt;serviceBehaviors&gt;
              &lt;behavior name=""&gt;
                  &lt;serviceMetadata httpGetEnabled="true" /&gt;
                  &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
              &lt;/behavior&gt;
          &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>I'm trying to call Foo from a page via jQuery:</p>
  
  <pre><code>&lt;script type="text/javascript" language="javascript"&gt;
      $(document).ready(function () {
          $("#msgButton").click(function () {
              var params = {};
              params.name = $("#nameTextbox").val();
  
              $.ajax({
                  type: 'POST',
                  url: "http://acme.com/wcfsvc/FooService.svc/Foo",
                  data: JSON.stringify(params),
                  contentType: 'application/json; charset=utf-8',
                  success: function (response, status, xhr) { alert('success: ' + response); },
                  error: function (xhr, status, error) { alert("Error\n-----\n" + xhr.status + '\n' + xhr.responseText); },
                  complete: function (jqXHR, status) { alert('Status: ' + status + '\njqXHR: ' + JSON.stringify(jqXHR)); }
              });
          });
      });        
  &lt;/script&gt;
  </code></pre>
  
  <p>But I'm getting a <strong>400 - Bad Request</strong> error with the message <strong>"The server encountered an error processing the request. The exception message is 'There was an error deserializing the object of type System.String. End element 'root' from namespace '' expected. Found element 'name' from namespace"</strong>.</p>
  
  <p>Am I missing something?</p>