我的jQuery客户端和WCF SOAP Web服务之间的“404 Not Found”

时间:2010-09-08 16:02:52

标签: jquery wcf soap

有人能发现我的代码有什么问题吗?当我使用jQuery调用WCF SOAP服务时,我在firebug上找到404 Not Found。

我使用IIS7在Win7上。我将wcf作为(http://localhost/csw)在虚拟目录应用程序上运行。我可以通过此URL访问service.svc文件,没有任何问题:(http://localhost/csw/service.svc

这是配置标记之间的Web.config

<configuration>
<system.web>
    <compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name ="soapBinding">
      <security mode="None">
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <services>
        <service name="CatalogService" behaviorConfiguration="defaultBehavior">  
    <endpoint address="soap"
              binding="basicHttpBinding"
              bindingConfiguration="soapBinding"
              contract="ICatalogService" />
        </service>
    </services>
    <behaviors>
        <endpointBehaviors>
            <behavior name="xmlBehavior">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>    
            <behavior name="defaultBehavior">
                <!-- 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>
    </behaviors>
</system.serviceModel>
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

App_Code文件/ ICatalogServices.cs:

[ServiceContract(Namespace = "http://test/CatalogService")] public interface ICatalogService {
[WebInvoke(Method = "POST",
             BodyStyle = WebMessageBodyStyle.Wrapped,
             ResponseFormat = WebMessageFormat.Xml,
             RequestFormat = WebMessageFormat.Xml)]
string HelloWorld(string name);}

App_Code文件/ CatalogServices.cs:

public class CatalogService : ICatalogService{
public string HelloWorld(string name){
    return String.Format("Hello {0}", name);}}

jQuery调用:

    $.ajax({
    type: 'POST',
    url: 'http://localhost/csw/service.svc/HelloWorld',
    data: request,
    contentType: 'application/xml; charset=utf-8',
    dataType: 'xml',
    success: function (result) {
        console.log(result);

        $("#result").text(result);
        //result.responseXML
        //result.responseText
    },
    error: function (message) {
        console.log(message);
        alert("error has occured" + message);
    }
});

我的请求xml是:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  <s:Body>  <HelloWorld xmlns="http://test/CatalogService">  <name>CarlosK</name>  </HelloWorld>  </s:Body>  </s:Envelope>

1 个答案:

答案 0 :(得分:3)

jQuery代码中的URL错误。尝试使用http://localhost/csw/service.svc/soap。还要将内容类型更改为text / xml; charset = utf-8

修改

地址:调用SOAP服务时,操作名称不是URL的一部分。 (与REST服务相反)。在您的配置中,您还定义了SOAP端点的相对地址。有效的URL是BaseAddress + /service.svc + / RelativeAddress。基于地址由您的虚拟目录定义。

内容类型:您正在BasicHttpBinding上公开该服务。 BasicHttpBinding使用SOAP 1.1。 SOAP 1.1的正确内容类型是text / xml和charset。

编辑新错误:

新错误表示无法将空操作路由到您的服务中的操作。您必须通过jQuery将SOAPAction HTTP标头添加到您的请求构建中。标头的值应为http://test/CatalogService/ICatalogService/HelloWorld