title: Ajax call to WCF Web Service returns 400 Bad Request
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/10986479/wcf-web-service-returns-bad-request-error-when-invoked-through-javascript">WCF Web Service returns &ldquo;Bad Request&rdquo; error when invoked through Javascript</a>  </p>
  </blockquote>
  
  
  
  <p>I have a simple WCF web service Service1 with just one method Test() which returns a string. I have deployed this web service on a test machine, but when I try to invoke the Test method from the browser I simply get a 400 Bad Request error. The same happens when I try to invoke the method through an AJAX GET request. But surprisingly, the method returns the correct result when invoked through the WCFTestClient.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>[ServiceContract]
  public interface IService1
  {
      // This method can be called to get a list of page sets per report.
      [OperationContract]
      [WebGet]
      string Test();
  }       
  
  public class Service1 : IService1
  {
      public string Test()
      {
          return "test";
      }
  }
  </code></pre>
  
  <p>This is my AJAX request:</p>
  
  <pre><code> var serverUrl1 = 'http://' + baseUrl + ':86/Service1.svc';
  
   function GetTestString()
   {
          var methodUrl = serverUrl1 + "/Test";
          $.ajax({
      async: false,
                  type: "GET",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
              url: methodUrl,
                  beforeSend: function (XMLHttpRequest) {
          //ensures the results will be returned as JSON.
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
                  },
      success: function (data) {
          ShowQRGSlides(data.d);
      },
      error: function (XmlHttpRequest, textStatus, errorThrown) {
          alert("ERROR: GetAvailablePages() Check your browsers javascript console for more details." + " \n XmlHttpRequest: " + XmlHttpRequest + " \n textStatus: " + textStatus + " \n errorThrown: " + errorThrown);
      }
    });
  
   }
  </code></pre>
  
  <p>Here is the web.config file for my web service:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
  
  &lt;system.web&gt;
  &lt;compilation debug="true" targetFramework="4.0" /&gt;
  &lt;customErrors mode="Off"/&gt;
  &lt;/system.web&gt;
  &lt;system.serviceModel&gt;
  &lt;behaviors&gt;
    &lt;serviceBehaviors&gt;
      &lt;behavior&gt;
        &lt;!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --&gt;
        &lt;serviceMetadata httpGetEnabled="true"/&gt;
        &lt;!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information --&gt;
        &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt;
      &lt;/behavior&gt;
    &lt;/serviceBehaviors&gt;
  &lt;/behaviors&gt;
  &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt;
  &lt;/system.serviceModel&gt;
  &lt;system.webServer&gt;
  &lt;modules runAllManagedModulesForAllRequests="true"/&gt;
  &lt;/system.webServer&gt;
  
  &lt;/configuration&gt;
  </code></pre>
  
  <p>This is just the simple web.config which gets automatically generated. I am unable to figure out why this simple web service method is not accessible through the browser or ajax. The same method returns the result when accessed through the WCFTestClient.</p>
  
  <p>Any inputs will be greatly appreciated! Thanks.</p>