【问题标题】:Jquery Ajax and WCF Webservice nightmareJquery Ajax 和 WCF Web 服务的噩梦
【发布时间】:2012-01-27 22:01:28
【问题描述】:

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

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

此时我不介意将整个内容转换为 JSON。我只需要让它工作。

当我使用 WCF 测试客户端 (Visual Studio 2010) 时,我的 Web 服务运行良好。但我就是无法让 JQuery AJAX 与之对话。我收到错误请求错误。

如果您需要更多详细信息,请随时询问。

这是我的网络服务 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>

【问题讨论】:

    标签: xml json wcf jquery


    【解决方案1】:

    您的 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 响应。

    【讨论】:

    • 你好罗科!!!我在原始问题中添加了我的 web.config。我想我已经正确配置了它。你怎么看?
    • 这看起来不错。您的服务OperationContracts 有哪些属性?查看我的编辑。
    • 罗科!!!我的错。我在我的 jquery 方法中有:“POST”而不是 type:“Post”.... :( 今日白痴代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多