从jQuery调用自托管的WCF不起作用

时间:2015-03-12 06:38:46

标签: jquery asp.net wcf

我通过Windows服务拥有自托管的WCF。我试图在服务中调用一个方法,但它不起作用。我可以在Google Chrome控制台中看到以下错误

  

选项http://localhost:9093/Service1/method1

XMLHttpRequest cannot load http://localhost:9093/Service1/method1. Invalid HTTP status code 400

这是我的jQuery代码

var url = 'http://localhost:9093/Service1/method1';
                $.ajax({
                    url: url,
                    data: { identifire: identifire },
                    type: 'POST',
                    global: false,
                    dataType: 'json',
                    contentType: "application/json; charset=utf-8",
                    success: callbacks.success,
                    error: function () { }
                })

这是我的合同

[OperationContract]
        [WebInvoke(Method = "POST",
            BodyStyle = WebMessageBodyStyle.Wrapped,
            ResponseFormat = WebMessageFormat.Json)]
        void method1(string identifire);

这是配置文件

 <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webSupport">
          <webHttp />

        </behavior>
      </endpointBehaviors>
    </behaviors>

    <services>
      <service name="IService1" behaviorConfiguration="ServiceBehavior">
        <endpoint address="http://localhost:9093/Service1" behaviorConfiguration="webSupport"
            binding="webHttpBinding" bindingConfiguration="" contract="IService1" />
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel>

</configuration>

这是我在Windows服务的OnStart事件中启动服务的方式

string hostAddress = "http://localhost:9093/Service1";

        Uri[] addresses = { new Uri(hostAddress) };

        scanHost = new ServiceHost(typeof(Service1),addresses);
        scanHost.Open();

BTW当我使用ASP.net的服务引用调用此方法时,它工作正常。尝试从jQuery中调用它时,我只会遇到问题

1 个答案:

答案 0 :(得分:0)

像这样更改你的终端标签

<endpoint address="" behaviorConfiguration="webSupport"
        binding="webHttpBinding" bindingConfiguration="" contract="IService1" />

此外,您的网址应该是这样的

var url = 'http://localhost:9093/Service1.svc/method1';

数据应该是这样的

data: JSON.stringify({identifire: identifire}),