【问题标题】:Invoke self hosted WCF from jQuery not working从 jQuery 调用自托管 WCF 不起作用
【发布时间】:2015-05-14 05:29:56
【问题描述】:

我有一个通过 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();

顺便说一句,当我使用来自 ASP.net 的服务引用调用此方法时,它工作正常。我只在尝试从 jQuery 调用它时遇到问题

【问题讨论】:

    标签: jquery asp.net wcf


    【解决方案1】:

    像这样更改您的端点标签

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

    你的网址也应该是这样的

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

    而且数据应该是这样的

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

    【讨论】:

    • identifire 的价值是什么?
    猜你喜欢
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    相关资源
    最近更新 更多