Jquery Ajax和WCF Web Service的噩梦

时间:2012-01-27 22:01:28

标签: xml json wcf jquery

我创建了一个WCF Webservice方法,它接收两个参数string,并返回一个XmlElement。

我有一个ASP.NET页面,其中包含对该方法的JQuery AJAX调用。它不起作用,在线文档只是让我更加困惑。要记住的一些方面是,现在我们将参数发送为url-encoded。

我不介意在这一点上将整个事情变成JSON。我只需要让它工作。

当我使用WCF测试客户端(Visual Studio 2010)时,我的web服务工作正常。但我无法让JQuery AJAX与之交谈。我收到错误请求错误。

如果您需要更多详情,请不要犹豫。

这是我的webservice的WSDL:

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <wsdl:types>
    <xsd:schema targetNamespace="http://tempuri.org/Imports">
      <xsd:import namespace="http://tempuri.org/" 
           schemaLocation="http://localhost:16859/AddressVerification.svc?xsd=xsd0"/>
      <xsd:import namespace="http://schemas.microsoft.com/2003/10/Serialization/" 
           schemaLocation="http://localhost:16859/AddressVerification.svc?xsd=xsd1"/>
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="IAddressVerification_DoWork_InputMessage">
    <wsdl:part name="parameters" element="tns:DoWork"/>
  </wsdl:message>
  <wsdl:message name="IAddressVerification_DoWork_OutputMessage">
    <wsdl:part name="parameters" element="tns:DoWorkResponse"/>
  </wsdl:message>
  <wsdl:message name="IAddressVerification_VerifyAddress_InputMessage">
    <wsdl:part name="parameters" element="tns:VerifyAddress"/>
  </wsdl:message>
  <wsdl:message name="IAddressVerification_VerifyAddress_OutputMessage">
    <wsdl:part name="parameters" element="tns:VerifyAddressResponse"/>
  </wsdl:message>
  <wsdl:portType name="IAddressVerification">
    <wsdl:operation name="DoWork">
      <wsdl:input message="tns:IAddressVerification_DoWork_InputMessage" 
            wsaw:Action="http://tempuri.org/IAddressVerification/DoWork"/>
      <wsdl:output message="tns:IAddressVerification_DoWork_OutputMessage" 
            wsaw:Action="http://tempuri.org/IAddressVerification/DoWorkResponse"/>
    </wsdl:operation>
    <wsdl:operation name="VerifyAddress">
      <wsdl:input message="tns:IAddressVerification_VerifyAddress_InputMessage" 
            wsaw:Action="http://tempuri.org/IAddressVerification/VerifyAddress"/>
      <wsdl:output message="tns:IAddressVerification_VerifyAddress_OutputMessage"
            wsaw:Action="http://tempuri.org/IAddressVerification/VerifyAddressResponse"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="BasicHttpBinding_IAddressVerification" 
        type="tns:IAddressVerification">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="DoWork">
      <soap:operation style="document" 
            soapAction="http://tempuri.org/IAddressVerification/DoWork"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="VerifyAddress">
      <soap:operation style="document" 
            soapAction="http://tempuri.org/IAddressVerification/VerifyAddress"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="AddressVerification">
    <wsdl:port name="BasicHttpBinding_IAddressVerification" 
          binding="tns:BasicHttpBinding_IAddressVerification">
      <soap:address location="http://localhost:16859/AddressVerification.svc"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

这是我的JQuery:

$(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 + "&city=" + city + "&state=" + state + "&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
        });
    });
});

这是我的web.config

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- 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 -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="EndpBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="Service">
        <endpoint address="" binding="webHttpBinding"
            contract="IAddressVerification" behaviorConfiguration="EndpBehavior"/>
      </service>
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

1 个答案:

答案 0 :(得分:0)

您的WCF端点配置为使用哪些绑定?

查看您的WSDL,看起来您正在使用使用SOAP的绑定 - 这不会影响您尝试通过jQuery提交数据的方式。

看看使用webHttpBinding。这将使服务能够响应简单的Web请求。

http://msdn.microsoft.com/en-us/library/bb412176.aspx

编辑:你还想用以下属性装饰你的AddressVerification OperationContract:

[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "AddressVerification ")]

这将在地址“http:// localhost:16859 / AddressVerification.svc / AddressVerification”中公开该方法并返回JSON响应。