0

以下是我的 WCF 服务的 WSDL 代码,如何从 jQuery Ajax 调用服务 Directory.svc?我尝试调用“Directory.svc/LoadStaff”和“Directory.svc/Directory/LoadStaff”,但收到 HTTP 400 错误。

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions name="Directory" 
                  targetNamespace="Directory" 
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                  xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy"
                  xmlns:wsa10="http://www.w3.org/2005/08/addressing"
                  xmlns:tns="Directory" 
                  xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract"
                  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
                  xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" 
                  xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
                  xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
                  xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl"
                  xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
                  xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" 
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <wsdl:types>
    <xs:schema elementFormDefault="qualified" 
               targetNamespace="Directory" 
               xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:import namespace="http://schemas.microsoft.com/Message"/>
      <xs:element name="LoadStaff">
        <xs:complexType>
          <xs:sequence>
            <xs:element minOccurs="0" 
                        name="domainpath" 
                        nillable="true" 
                        type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="LoadStaffResponse">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="LoadStaffResult" 
                        type="q1:StreamBody" 
                        xmlns:q1="http://schemas.microsoft.com/Message"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    <xs:schema attributeFormDefault="qualified" 
               elementFormDefault="qualified"
               targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/">
      <xs:element name="anyType" 
                  nillable="true" 
                  type="xs:anyType"/>
      <xs:element name="anyURI" 
                  nillable="true" 
                  type="xs:anyURI"/>
      <xs:element name="base64Binary" 
                  nillable="true" 
                  type="xs:base64Binary"/>
      <xs:element name="boolean" 
                  nillable="true" 
                  type="xs:boolean"/>
      <xs:element name="byte" 
                  nillable="true" 
                  type="xs:byte"/>
      <xs:element name="dateTime" 
                  nillable="true" 
                  type="xs:dateTime"/>
      <xs:element name="decimal" 
                  nillable="true" 
                  type="xs:decimal"/>
      <xs:element name="double" 
                  nillable="true" 
                  type="xs:double"/>
      <xs:element name="float" 
                  nillable="true" 
                  type="xs:float"/>
      <xs:element name="int" 
                  nillable="true" 
                  type="xs:int"/>
      <xs:element name="long" 
                  nillable="true" 
                  type="xs:long"/>
      <xs:element name="QName" 
                  nillable="true" 
                  type="xs:QName"/>
      <xs:element name="short" 
                  nillable="true" 
                  type="xs:short"/>
      <xs:element name="string" 
                  nillable="true" 
                  type="xs:string"/>
      <xs:element name="unsignedByte" 
                  nillable="true" 
                  type="xs:unsignedByte"/>
      <xs:element name="unsignedInt" 
                  nillable="true" 
                  type="xs:unsignedInt"/>
      <xs:element name="unsignedLong" 
                  nillable="true" 
                  type="xs:unsignedLong"/>
      <xs:element name="unsignedShort" 
                  nillable="true" 
                  type="xs:unsignedShort"/>
      <xs:element name="char" 
                  nillable="true" 
                  type="tns:char"/>
      <xs:simpleType name="char">
        <xs:restriction base="xs:int"/>
      </xs:simpleType>
      <xs:element name="duration" 
                  nillable="true" 
                  type="tns:duration"/>
      <xs:simpleType name="duration">
        <xs:restriction base="xs:duration">
          <xs:pattern value="\-?P(\d*D)?(T(\d*H)?(\d*M)?(\d*(\.\d*)?S)?)?"/>
          <xs:minInclusive value="-P10675199DT2H48M5.4775808S"/>
          <xs:maxInclusive value="P10675199DT2H48M5.4775807S"/>
        </xs:restriction>
      </xs:simpleType>
      <xs:element name="guid" 
                  nillable="true" 
                  type="tns:guid"/>
      <xs:simpleType name="guid">
        <xs:restriction base="xs:string">
          <xs:pattern value="[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}"/>
        </xs:restriction>
      </xs:simpleType>
      <xs:attribute name="FactoryType" 
                    type="xs:QName"/>
      <xs:attribute name="Id" 
                    type="xs:ID"/>
      <xs:attribute name="Ref" 
                    type="xs:IDREF"/>
    </xs:schema>
    <xs:schema elementFormDefault="qualified"
               targetNamespace="http://schemas.microsoft.com/Message" 
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:tns="http://schemas.microsoft.com/Message">
      <xs:simpleType name="StreamBody">
        <xs:restriction base="xs:base64Binary"/>
      </xs:simpleType>
    </xs:schema>
  </wsdl:types>
  <wsdl:message name="Directory_LoadStaff_InputMessage">
    <wsdl:part name="parameters" 
               element="tns:LoadStaff"/>
  </wsdl:message>
  <wsdl:message name="Directory_LoadStaff_OutputMessage">
    <wsdl:part name="parameters" 
               element="tns:LoadStaffResponse"/>
  </wsdl:message>
  <wsdl:portType name="Directory">
    <wsdl:operation name="LoadStaff">
      <wsdl:input wsaw:Action="Directory/Directory/LoadStaff" 
                  message="tns:Directory_LoadStaff_InputMessage"/>
      <wsdl:output wsaw:Action="Directory/Directory/LoadStaffResponse"
                   message="tns:Directory_LoadStaff_OutputMessage"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="BasicHttpBinding_Directory" 
                type="tns:Directory">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="LoadStaff">
      <soap:operation soapAction="Directory/Directory/LoadStaff" 
                      style="document"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="Directory">
    <wsdl:port name="BasicHttpBinding_Directory" 
               binding="tns:BasicHttpBinding_Directory">
      <soap:address location="http://localhost:59440/Directory.svc"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

这是我用来调用服务的 jQuery 代码:

$.ajax({
url: 'http://localhost:59440/Directory.svc/LoadStaff',
type: 'GET',
dataType: "json",
success: function (data) {
//nothing to do yet
},
error: function (SOAPResponse) {
    console.log('oops');
}

});

4

1 回答 1

0

看起来您正在向基于 SOAP 的服务发出 HTTP GET 请求,该服务本质上总是期待基于 XML 的 SOAP 信封的 HTTP POST。

由于您的客户端使用 javascript,因此您最好的选择可能是使用 WCF WebGet 或通过将框架切换到 ASP.NET WEB API 向服务添加基于 REST 的端点。

如果由于某种原因无法执行上述操作,您可以让您的 javascript 客户端实际进行 SOAP 调用。请注意,这会给您留下一个很难维护的客户端实现,还会让您陷入其他问题,例如错误处理等。

但是,如果您必须这样做,这可能会帮助您朝着正确的方向前进:

var yourSoapEnvelope = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/X......'

$.ajax({
url: yourURL,
type: "POST",
dataType: "xml",
data: yourSoapEnvelope,
complete: doStuff,
contentType: "text/xml; charset=\"utf-8\""
});
于 2013-09-16T10:29:55.920 回答