title: WCF Web Service returns "Bad Request" error when invoked through Javascript
body: <p>I am trying to make an ajax GET request to a WCF web service method through javascript. The request returns with a 400 "Bad Request" error everytime. But if I invoke the same web service method through the WCF Test Client, it returns the correct results. </p>
  
  <p>Also, the ajax call through javascript returns the bad request error on both GET and POST requests.</p>
  
  <p>I have added the [WebGet] attribute to my web service method but the error still persists. </p>
  
  <p>Here is my javascript method and ajax call:</p>
  
  <pre><code>function Getdata()
  {
  var methodUrl = serverUrl + "/GetData";
        $.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) {
  
      },
      error: function (XmlHttpRequest, textStatus, errorThrown) {
          alert("ERROR: GetData() Check your browsers javascript console for more details." + " \n XmlHttpRequest: " + XmlHttpRequest + " \n textStatus: " + textStatus + " \n errorThrown: " + errorThrown);
      }
  });
  }
  </code></pre>
  
  <p>Here, serverUrl is of the format: "http://hostname:portnumber/webServiceProxy.svc"</p>
  
  <p>Any idea why the request would return a "Bad Request" error when invoked through javascript and work perfectly when invoked through the WCF Test Client? Any inputs will be appreciated! Thank you!</p>
  
  <p>Also, I have compared the requests sent out from the WCF Test Client and from my AJAX request using Fiddler and here is what I see:</p>
  
  <p>WCF Test Client Request:</p>
  
  <pre><code>POST http://hostname:portnumber/WebServiceProxy.svc HTTP/1.1
  Content-Type: text/xml; charset=utf-8
  SOAPAction: "http://tempuri.org/IWebServiceProxy/GetData"
  Host: hostname:portnumber
  Content-Length: 144
  Expect: 100-continue
  Accept-Encoding: gzip, deflate
  
  &lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;s:Body&gt;&lt;GetData xmlns="http://tempuri.org/"/&gt;&lt;/s:Body&gt;&lt;/s:Envelope&gt;
  </code></pre>
  
  <p>My AJAX Request:</p>
  
  <pre><code>GET http://hostname:portnumber/WebServiceProxy.svc/GetData HTTP/1.1
  Content-Type: application/json; charset=utf-8
  Accept-Language: en-us
  Referer: http://hostname/OrgName/WebResources/resourceName?pagemode=iframe
  Accept: application/json
  Accept-Encoding: gzip, deflate
  User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; InfoPath.3; .NET4.0E; BOIE9;ENUS)
  </code></pre>
  
  <p>Host: hostname:portnumber
      Connection: Keep-Alive</p>
  
  <p>So the biggest difference that I see is that the WCF Test Client makes a POST request and sends the web service method name in a SOAP envelope while my ajax request is a GET and adds the web service method name to the url. Looking at these requests, any ideas on how I might have to change my ajax call to get it to work correctly? Thanks!</p>
  
  <p>Editing to add my web.config file contents:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&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;bindings&gt;
          &lt;wsHttpBinding&gt;
              &lt;binding name="WSHttpBinding_IDDSWCFService" closeTimeout="00:01:00"
                  openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                  bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                  maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                  messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                  allowCookies="false"&gt;
                  &lt;readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                      maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;
                  &lt;reliableSession ordered="true" inactivityTimeout="00:10:00"
                      enabled="false" /&gt;
                  &lt;security mode="Message"&gt;
                      &lt;transport clientCredentialType="Windows" proxyCredentialType="None"
                          realm="" /&gt;
                      &lt;message clientCredentialType="Windows" negotiateServiceCredential="true"
                          algorithmSuite="Default" /&gt;
                  &lt;/security&gt;
              &lt;/binding&gt;
          &lt;/wsHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;client&gt;
          &lt;endpoint address="http://machinename:8080/DDSWCFService" binding="wsHttpBinding"
              bindingConfiguration="WSHttpBinding_IDDSWCFService" contract="WCFService.IDDSWCFService"
              name="WSHttpBinding_IDDSWCFService"&gt;
              &lt;identity&gt;
  
              &lt;/identity&gt;
          &lt;/endpoint&gt;
      &lt;/client&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="true"/&gt;
              &lt;/behavior&gt;
  
          &lt;/serviceBehaviors&gt;
  
          &lt;endpointBehaviors&gt;
              &lt;behavior name="webHttpBehavior"&gt;
                  &lt;enableWebScript/&gt;
                  &lt;webHttp helpEnabled="true"/&gt;
              &lt;/behavior&gt;
          &lt;/endpointBehaviors&gt;
  
      &lt;/behaviors&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p></p>