title: Jquery Ajax and WCF Webservice nightmare
body: <p>I created a WCF Webservice method that receives two parameters, string, and return an XmlElement.</p>
  
  <p>I have an ASP.NET page with a JQuery AJAX call to that method. It is not working and the documentation online is just getting me more confused by the minute. Some aspects to keep in mind is that right now we are sending the parameters as url-encoded.</p>
  
  <p>I wouldn't mind at this point to make the whole thing into JSON. I just need to get this working.</p>
  
  <p>My webservice works perfect when I use the WCF Test client (Visual Studio 2010). But I just can't get JQuery AJAX to talk to it. I get a Bad Request error.</p>
  
  <p>If you need more details, don't hesitate to ask.</p>
  
  <p>Here is my WSDL for the webservice:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;wsdl:definitions xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata"
        xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex"
        xmlns:wsa10="http://www.w3.org/2005/08/addressing" 
        xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" 
        xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" 
        xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" 
        xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" 
        xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
        xmlns:tns="http://tempuri.org/" 
        xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" 
        xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
        xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
        xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
        targetNamespace="http://tempuri.org/" 
        name="AddressVerification"&gt;
    &lt;wsdl:types&gt;
      &lt;xsd:schema targetNamespace="http://tempuri.org/Imports"&gt;
        &lt;xsd:import namespace="http://tempuri.org/" 
             schemaLocation="http://localhost:16859/AddressVerification.svc?xsd=xsd0"/&gt;
        &lt;xsd:import namespace="http://schemas.microsoft.com/2003/10/Serialization/" 
             schemaLocation="http://localhost:16859/AddressVerification.svc?xsd=xsd1"/&gt;
      &lt;/xsd:schema&gt;
    &lt;/wsdl:types&gt;
    &lt;wsdl:message name="IAddressVerification_DoWork_InputMessage"&gt;
      &lt;wsdl:part name="parameters" element="tns:DoWork"/&gt;
    &lt;/wsdl:message&gt;
    &lt;wsdl:message name="IAddressVerification_DoWork_OutputMessage"&gt;
      &lt;wsdl:part name="parameters" element="tns:DoWorkResponse"/&gt;
    &lt;/wsdl:message&gt;
    &lt;wsdl:message name="IAddressVerification_VerifyAddress_InputMessage"&gt;
      &lt;wsdl:part name="parameters" element="tns:VerifyAddress"/&gt;
    &lt;/wsdl:message&gt;
    &lt;wsdl:message name="IAddressVerification_VerifyAddress_OutputMessage"&gt;
      &lt;wsdl:part name="parameters" element="tns:VerifyAddressResponse"/&gt;
    &lt;/wsdl:message&gt;
    &lt;wsdl:portType name="IAddressVerification"&gt;
      &lt;wsdl:operation name="DoWork"&gt;
        &lt;wsdl:input message="tns:IAddressVerification_DoWork_InputMessage" 
              wsaw:Action="http://tempuri.org/IAddressVerification/DoWork"/&gt;
        &lt;wsdl:output message="tns:IAddressVerification_DoWork_OutputMessage" 
              wsaw:Action="http://tempuri.org/IAddressVerification/DoWorkResponse"/&gt;
      &lt;/wsdl:operation&gt;
      &lt;wsdl:operation name="VerifyAddress"&gt;
        &lt;wsdl:input message="tns:IAddressVerification_VerifyAddress_InputMessage" 
              wsaw:Action="http://tempuri.org/IAddressVerification/VerifyAddress"/&gt;
        &lt;wsdl:output message="tns:IAddressVerification_VerifyAddress_OutputMessage"
              wsaw:Action="http://tempuri.org/IAddressVerification/VerifyAddressResponse"/&gt;
      &lt;/wsdl:operation&gt;
    &lt;/wsdl:portType&gt;
    &lt;wsdl:binding name="BasicHttpBinding_IAddressVerification" 
          type="tns:IAddressVerification"&gt;
      &lt;soap:binding transport="http://schemas.xmlsoap.org/soap/http"/&gt;
      &lt;wsdl:operation name="DoWork"&gt;
        &lt;soap:operation style="document" 
              soapAction="http://tempuri.org/IAddressVerification/DoWork"/&gt;
        &lt;wsdl:input&gt;
          &lt;soap:body use="literal"/&gt;
        &lt;/wsdl:input&gt;
        &lt;wsdl:output&gt;
          &lt;soap:body use="literal"/&gt;
        &lt;/wsdl:output&gt;
      &lt;/wsdl:operation&gt;
      &lt;wsdl:operation name="VerifyAddress"&gt;
        &lt;soap:operation style="document" 
              soapAction="http://tempuri.org/IAddressVerification/VerifyAddress"/&gt;
        &lt;wsdl:input&gt;
          &lt;soap:body use="literal"/&gt;
        &lt;/wsdl:input&gt;
        &lt;wsdl:output&gt;
          &lt;soap:body use="literal"/&gt;
        &lt;/wsdl:output&gt;
      &lt;/wsdl:operation&gt;
    &lt;/wsdl:binding&gt;
    &lt;wsdl:service name="AddressVerification"&gt;
      &lt;wsdl:port name="BasicHttpBinding_IAddressVerification" 
            binding="tns:BasicHttpBinding_IAddressVerification"&gt;
        &lt;soap:address location="http://localhost:16859/AddressVerification.svc"/&gt;
      &lt;/wsdl:port&gt;
    &lt;/wsdl:service&gt;
  &lt;/wsdl:definitions&gt;
  </code></pre>
  
  <p>Here is my JQuery:</p>
  
  <pre><code>$(document).ready(function () {
  
      $("#btnSubmit").click(function (e) {
          e.preventDefault();
          var address1 = $("#txtAddress1").val();
          var address2 = $("#txtAddress2").val();
          var city = $("#txtCity").val();
          var state = $("#txtState").val();
          var zipcode = $("#txtZipcode").val();
  
          var dataToSend = "address=" + address1 + "&amp;city=" + city + "&amp;state=" + state + "&amp;zip=" + zipcode;
  
          $.ajax(
          {
              method: "POST",
              url: "http://localhost:16859/AddressVerification.svc/AddressVerification",
              data: dataToSend,
              success: function (dataReturned) {
                  alert("YAY!!!!!!!");
              },
              error: function (dataReturned, status, errorT) {
                  alert('error thrown: ' + errorT);
                  alert('status: ' + status);
                  alert('dataReturned: ' + dataReturned.toString());
              },
              cache: false
          });
      });
  });
  </code></pre>
  
  <p>Here is my web.config</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;/system.web&gt;
    &lt;system.serviceModel&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="ServiceBehavior"&gt;
            &lt;serviceMetadata httpGetEnabled="true"/&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
          &lt;/behavior&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;endpointBehaviors&gt;
          &lt;behavior name="EndpBehavior"&gt;
            &lt;webHttp/&gt;
          &lt;/behavior&gt;
        &lt;/endpointBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;services&gt;
        &lt;service behaviorConfiguration="ServiceBehavior" name="Service"&gt;
          &lt;endpoint address="" binding="webHttpBinding"
              contract="IAddressVerification" behaviorConfiguration="EndpBehavior"/&gt;
        &lt;/service&gt;
      &lt;/services&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>