簡體   English   中英

從jQuery調用自托管WCF無效

[英]Invoke self hosted WCF from jQuery not working

我有一個通過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的Service引用調用此方法時,它工作正常。 嘗試從jQuery調用時,我只會遇到問題

像這樣更改端點標記

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

另外你的網址應該是這樣的

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

數據應該是這樣的

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM