2010-09-10 94 views
3

我想從jquery調用wcf自我託管的方法,但是,我總是得到消息:「方法不允許」。我發現這個論壇上的這個問題的一些答案,但沒有對我工作.... PS。當我在控制檯應用程序中添加引用並使用它時,它工作正常。wcf自我託管jquery

它是一個窗口形成自託管應用程序

形式負載:

ServiceHost host = new ServiceHost(typeof(MyServices), new Uri[] { }); 
host.Open(); 

App.Config中

<system.serviceModel> 
     <services> 
      <service behaviorConfiguration="ServiceConfig" name="MyServices"> 
       <endpoint address="srv" binding="basicHttpBinding" contract="Operations" /> 
       <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
       <host> 
        <baseAddresses> 
         <add baseAddress="http://localhost:8766" /> 
        </baseAddresses> 
       </host> 
      </service> 
     </services> 
     <behaviors> 
      <serviceBehaviors> 
       <behavior name="ServiceConfig"> 
        <serviceMetadata httpGetEnabled="true" /> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 
     <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
    </system.serviceModel> 

經營合同:

[OperationContract] 
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
string Test(); 

服務:

public string Test() 
{ 
return "Good!"; 
} 

JQuery的

$.ajax({ 
    url: "http://localhost:8766/Test", 
    contentType: "application/json", 
    processData: false, 
    //data: '{}', // tried passing data 
    type: "GET", // tried POST and pass nothing(deleting this param), but nothing... 
    success: function (msg) { 
     console.log(msg); 
    } 
}); 

回答

2

那麼,當前的問題是由以下事實WebInvoke,當你不指定Method參數,將默認爲 「POST」 造成的。你的jQuery代碼發出一個「GET」請求,所以「方法不允許」錯誤將被期望。

但是,您最初嘗試GET並得不到任何回報的評論使我傾向於使用瀏覽器違反same origin policy。由於您在Windows窗體中使用地址http://localhost:8766託管服務,因此我假設您的網站不在'http://localhost:8766'處。每同源策略:

術語「原產地」使用 域名的定義,應用層 協議,並(在大多數瀏覽器)TCP 運行 腳本的HTML文檔的端口。兩個資源被認爲是 是相同的來源當且僅當 如果所有這些值完全相同 相同。

如果您以JSON格式從服務中返回數據,則可能需要考慮使用返回JSONP的GET請求。這種類型的請求不會違反相同的源策略,因爲允許請求遠程腳本。然後,您的通話jQuery的是這樣的:

$.ajax({ 
    url: "http://localhost:8766/Test", 
    dataType: "jsonp", 
    processData: false, 
    type: "GET", 
    success: function (msg) { 
     console.log(msg); 
    } 
}); 

由於您使用WebInvoke,您可能還需要使用WebHttpBinding結合,而不是BasicHttpBindingWebInvoke不起作用,除非它與WebHttpBinding一起使用。而當您使用WebHttpBinding時,您可以自定義它以響應JSONP請求。所以,你的配置是這樣的:

<system.serviceModel> 
    <bindings> 
     <webHttpBinding> 
      <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" /> 
     </webHttpBinding> 
    </bindings> 
    <behaviors> 
     <endpointBehaviors> 
      <behavior name="webHttpBehavior"> 
      <webHttp /> 
      </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <services> 
     <service behaviorConfiguration="ServiceConfig" name="MyServices"> 
      <endpoint address="srv" binding="webHttpBinding" contract="Operations" bindingConfiguration="webHttpBindingWithJsonP" behaviorConfiguration="webHttpBehavior" /> 
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
      <host> 
       <baseAddresses> 
        <add baseAddress="http://localhost:8766" /> 
       </baseAddresses> 
      </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
      <behavior name="ServiceConfig"> 
       <serviceMetadata httpGetEnabled="true" /> 
      </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
</system.serviceModel> 

這裏是一個非常好的博客post,引導您完成這一點。

希望這有助於!