title: Make a WCF Service Accept JSON Data from jQuery.AJAX()
body: <p>I have been searching around for hours and trying different things to get this to work. I have tried so many articles on stackoverflow and either I am too stupid to get things working or I have some unique and odd configuration that is preventing me from experiencing joy.</p>
  
  <p>I create the WCF service outlined by this tutorial:</p>
  
  <p><a href="http://www.codeproject.com/Articles/97204/Implementing-a-Basic-Hello-World-WCF-Service" rel="nofollow noreferrer">http://www.codeproject.com/Articles/97204/Implementing-a-Basic-Hello-World-WCF-Service</a></p>
  
  <p>It is super basic and has one method and all I want it to do is allow me to consume it with jQuery.AJAX() using json.</p>
  
  <p>I have it hosted in IIS and it works. I can access the WSDL without issues.</p>
  
  <p>I try to consume it with the following code:</p>
  
  <pre><code>$.ajax({
      dataType: 'json',
      type: 'POST',
      contentType: "application/json",
      url: "//localhost:546/HelloWorldService.svc/GetMessage",
      data: {
          name: "Joe"
      }
      }).done(function(msg){
          console.log(msg);
          $("#result").append(msg);
  });
  </code></pre>
  
  <p>I always get errors. Based on what I have tried I get 500 errors, 402 errors, errors about incorrect content... All the errors.</p>
  
  <p>I have tried implementing solutions from the following articles. They range from having me change the web.config endpoints (I know I HAVE to change them but nothing I have tried so far works in terms of adding a JSON endpoint) to adding things like </p>
  
  <pre><code>[WebInvoke(Method = "POST", UriTemplate = "json/PostSalesOrderData", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
  </code></pre>
  
  <p>to the interface.</p>
  
  <p>Here are some of the articles I have looked at and tried to smash into my solution to get it working without much success.</p>
  
  <p><a href="https://stackoverflow.com/questions/14147259/javascript-json-and-wcf-webservice-on-phongap-android">Javascript JSON and WCF webservice on Phonegap Android</a></p>
  
  <p><a href="https://stackoverflow.com/questions/13617561/http-1-1-415-cannot-process-the-message-because-the-content-type-application-js">HTTP/1.1 415 Cannot process the message because the content type &#39;application/json; charset=utf-8&#39; was not the expected type &#39;text/xml; charset=utf-8&#39;</a></p>
  
  <p><a href="https://stackoverflow.com/questions/4688545/wcf-services-with-json-jsonp-and-soap-end-points">WCF Services with JSON, JSONP and SOAP End Points</a></p>
  
  <p><a href="https://stackoverflow.com/questions/7359759/two-endpoint-soap-json-and-one-service-method">Two endpoint (soap ,json) and one service method</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5006195/wcf-rest-service-not-accepting-json-in-net-4">WCF REST Service not accepting JSON in .Net 4</a></p>
  
  <p>I also went through this tutorial and tried to use what he had to say to get my solution working. Still nothing!</p>
  
  <p><a href="http://www.primordialcode.com/blog/post/passing-json-serialized-objects-wcf-service-jquery" rel="nofollow noreferrer">http://www.primordialcode.com/blog/post/passing-json-serialized-objects-wcf-service-jquery</a></p>
  
  <p>This is how my interface looks</p>
  
  <pre><code>[ServiceContract]
  public interface IHelloWorldService
  {
      [OperationContract]
      [WebInvoke(UriTemplate = "GetMessage", Method = "POST",
          BodyStyle = WebMessageBodyStyle.WrappedRequest,
          RequestFormat = WebMessageFormat.Json,
          ResponseFormat = WebMessageFormat.Json)]
      String GetMessage(String name);
  }
  </code></pre>
  
  <p>Can anyone help me experience joy?</p>
  
  <p>Thanks in advance for even looking at my question. If you need more information or I have not provided enough let me know so I can help you help me!</p>
  
  <p>I must be missing something stupid... I know it is not this hard.</p>
  
  <p>EDIT:</p>
  
  <p>Working Web.Config</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
    &lt;system.web&gt;
      &lt;compilation debug="false" targetFramework="4.0" /&gt;
    &lt;/system.web&gt;
    &lt;system.webServer&gt;
      &lt;modules runAllManagedModulesForAllRequests="true"/&gt;
    &lt;/system.webServer&gt;
    &lt;system.serviceModel&gt;
      &lt;behaviors&gt;
        &lt;endpointBehaviors&gt;
          &lt;behavior name="WebHTTPEndpointBehavior"&gt;
            &lt;webHttp /&gt;
          &lt;/behavior&gt;
        &lt;/endpointBehaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="MyServiceTypeBehaviors"&gt;
            &lt;serviceMetadata httpGetEnabled="true" /&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;bindings&gt;
        &lt;webHttpBinding&gt;
          &lt;binding name="MyWebServiceBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"/&gt;
        &lt;/webHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;services&gt;
        &lt;service name="MyWCFServices.HelloWorldService"
         behaviorConfiguration="MyServiceTypeBehaviors"&gt;
          &lt;endpoint address="" binding="webHttpBinding" bindingConfiguration="MyWebServiceBinding" behaviorConfiguration="WebHTTPEndpointBehavior"
            contract="MyWCFServices.IHelloWorldService"/&gt;
          &lt;endpoint contract="IMetadataExchange"
            binding="mexHttpBinding" address="mex"/&gt;
        &lt;/service&gt;
      &lt;/services&gt;
    &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>