title: Request JSON data in WCF REST Service and call through jQuery
body: <p>I am developing a scenario in which I have to insert the data in xml file.</p>
  
  <p>Following is my service contract:</p>
  
  <pre><code>namespace WcfService1
  {   
      [ServiceContract]
      public interface IService1
      {
         [OperationContract]
         [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "PostData")]
         string InsertData(Data data);
      }
  
      [DataContract]
      public class Data
      {
          [DataMember]
          public string Name { get; set; }
          [DataMember]
          public string Email { get; set; }
          [DataMember]
          public string Category { get; set; }
          [DataMember]
          public string Mobile { get; set; }
          [DataMember]
          public string Message { get; set; }
      }
  }
  </code></pre>
  
  <p>My Config file:</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;appSettings&gt;
  &lt;add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /&gt;
    &lt;/appSettings&gt;
    &lt;system.web&gt;
  &lt;compilation debug="true" targetFramework="4.5.1" /&gt;
  &lt;httpRuntime targetFramework="4.5.1"/&gt;
    &lt;/system.web&gt;
    &lt;system.serviceModel&gt;
      &lt;bindings&gt;
    &lt;webHttpBinding&gt;
      &lt;binding name="b_WebHttpBinding" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="Buffered"
      useDefaultWebProxy="true" crossDomainScriptAccessEnabled="true"&gt;
        &lt;readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
        maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /&gt;
        &lt;security mode="None"&gt;
          &lt;transport clientCredentialType="None" proxyCredentialType="None"
          realm="" /&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
    &lt;/webHttpBinding&gt;
  &lt;/bindings&gt;
     &lt;behaviors&gt;
    &lt;serviceBehaviors&gt;
      &lt;behavior name="ServiceBehaviour"&gt;
        &lt;serviceMetadata httpGetEnabled="true" /&gt;
        &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt;
      &lt;/behavior&gt;
    &lt;/serviceBehaviors&gt;
    &lt;endpointBehaviors&gt;
      &lt;behavior name="web"&gt;
        &lt;webHttp /&gt;
     &lt;/behavior&gt;
    &lt;/endpointBehaviors&gt;
  &lt;/behaviors&gt;
      &lt;services&gt;
        &lt;service behaviorConfiguration="ServiceBehaviour" name="WcfService1.Service1"&gt;
            &lt;endpoint address="WcfServiceAddress" behaviorConfiguration="web" binding="webHttpBinding"
    bindingConfiguration="b_WebHttpBinding" contract="WcfService1.IService1" /&gt;
    &lt;/service&gt;
      &lt;/services&gt;
      &lt;protocolMapping&gt;
      &lt;add binding="basicHttpsBinding" scheme="https" /&gt;
  &lt;/protocolMapping&gt;    
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /&gt;
    &lt;/system.serviceModel&gt;
    &lt;system.webServer&gt;
      &lt;modules runAllManagedModulesForAllRequests="true"/&gt;
  
      &lt;directoryBrowse enabled="true"/&gt;
    &lt;/system.webServer&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>I am calling it through the jQuery:</p>
  
  <pre><code>$(document).ready(function () {
          $('#BtnRegister').click(function () {
              debugger;
              var input =
                  {
                      data:
                          {
                              Name: document.getElementById("TxtUserName").value,
                              Email: document.getElementById("TxtUserEmail").value,
                              Category: document.getElementById("TxtUserCategory").value,
                              Mobile: document.getElementById("TxtUserMobile").value,
                              Message: document.getElementById("message").value,
                          }
                  };
              $.ajax({
  
                  type: "POST",
                  url: "http://localhost:22727/Service1.svc/WcfServiceAddress/PostData",
                  contentType: "application/json",
                  data: JSON.stringify(input),
                  success: function (result) {
                      alert("data");
                  },
                  error: function (xhr, textStatus, errorThrown) { alert(textStatus + ':' + errorThrown); }
              });
          });
      });
  </code></pre>
  
  <p>But the data is not inserting in the file.</p>