title: Calling WCF service from jquery Ajax
body: <p>I am trying to create a WCF service and call its methods using jquery AJAX. So far I have not being able to figure out how to do it. I have read some of the solutions given in SO but still unable to fix it. Any help would be appreciated. I have the following code.</p>
  
  <p>In WCF service.</p>
  
  <p>Service Contract</p>
  
  <pre><code>[ServiceContract]
  public interface IDatingService
  {
  [WebInvoke(Method = "POST",
  RequestFormat= WebMessageFormat.Json,
          ResponseFormat = WebMessageFormat.Json,
          BodyStyle = WebMessageBodyStyle.Wrapped,
          UriTemplate = "/AddUser")]
      [OperationContract]
      User AddUser(User user);
  }
  </code></pre>
  
  <p>Implementation</p>
  
  <pre><code>public User AddUser(User user)
  {
      return userRepository.Add(user);
  }
  </code></pre>
  
  <p>The configuration</p>
  
  <pre><code>&lt;system.serviceModel&gt;
   &lt;behaviors&gt;
    &lt;serviceBehaviors&gt;
      &lt;behavior name="DatingServiceBehavior"&gt;
        &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /&gt;
        &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt;
      &lt;/behavior&gt;
    &lt;/serviceBehaviors&gt;
    &lt;endpointBehaviors&gt;
      &lt;behavior name="DatingServiceBehavior"&gt;
        &lt;webHttp/&gt;
      &lt;/behavior&gt;
    &lt;/endpointBehaviors&gt;
  &lt;/behaviors&gt;
  &lt;services&gt;
    &lt;service behaviorConfiguration="DatingServiceBehavior"
             name="DatingService.Service.DatingService"&gt;
      &lt;endpoint address="" binding="webHttpBinding"
         contract="DatingService.Service.IDatingService"
         behaviorConfiguration="DatingServiceBehavior"/&gt;
      &lt;endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" /&gt;
    &lt;/service&gt;
   &lt;/services&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>From the client side, I use the following code to call the service.</p>
  
  <pre><code>saveInfo: function () {
      console.log('saveInfo');
      if ($('#terms-check').is(":checked")) {
          if (app.validateEmail()) {
              var input =
                  {
                      "user":
                      {
                          "LoginId": user.LoginId.text(),
                          "Password": user.Password.text(),
                          "Email": user.Email.val()
                      }
                  };
              $.ajax({
                  type: "POST",
                  url: "http://localhost:36908/DatingService.svc/AddUser",
                  data: JSON.stringify(input),
                  contentType: 'application/json; charset=utf-8',
                  dataType: "json",
                  success: app.onSaveSuccess,
                  error: app.onSaveError
              });
          }
          else {
              $('.error-label').text('Invalid email address.');
          }
      }
      else {
          $('.error-label').text('Please check the terms and conditions.');
      }
  }
  </code></pre>
  
  <p>When I call the service, I am getting Bad Request.</p>