2010-06-16 117 views
1

我創建了一個使用VS 2008的WCF SOAP服務,它用於服務器端。我加入了下面的代碼,使用jQuery/json(無ASP.NET腳本管理器)調用相同的服務/合同客戶端。當我將服務url放入瀏覽器時,我得到正確的服務頁面,當我從JavaScript調用服務並通過Firebug跟蹤時,我得到「405方法不允許」。jQuery Ajax調用WCF服務返回「方法不允許(405)」

ajax()錯誤函數的statusText和responseText不包含任何內容,並且出於某種奇怪的原因錯誤函數被調用兩次。我也從https頁面調用服務。

相同的ajax代碼與服務器端和客戶端的類似傳統web服務一起工作,也可以使用頁面方法。

任何線索?我還注意到我的Windows Communication Foundation HTTP激活和非HTTP激活未安裝。我需要這些嗎?儘管我的服務器端WCF服務工作。

接口:

[ServiceContract] 
public interface ICust 
{ 
    [OperationContract] 
    [WebInvoke(Method = "POST", 
     BodyStyle = WebMessageBodyStyle.Wrapped, 
     ResponseFormat = WebMessageFormat.Json, 
     RequestFormat = WebMessageFormat.Json)] 
    bool GetCust(string zzz); 
} 

類別:

public class Cust: ICust 
{ 
    public bool GetCust(string zzz) 
    { 
     // Do Stuff 
     // Return true/false; 
    } 
} 

的web.config:

<behaviors> 
    <serviceBehaviors> 
     <behavior name="E.W.CustomerBehavior" > 
      <serviceMetadata httpGetEnabled="true" /> 
     </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
     <behavior name="webCallable"> 
      <webHttp /> 
     </behavior>    
    </endpointBehaviors> 
</behaviors> 

<services> 
    <service behaviorConfiguration="E.W.CustomerBehavior" 
     name="E.W.Customer"> 
     <endpoint address="http://zzzz/Cust.svc" 
      binding="webHttpBinding" 
      contract="E.W.ICust" 
      behaviorConfiguration="webCallable"> 
      <identity> 
       <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" 
      binding="mexHttpBinding" 
      contract="IMetadataExchange" /> 
    </service> 
</services> 

的jQuery:

$.ajax({ 
    cache: false, 
    async: false, 
    type: "POST", 
    url: "http://zzzz/Cust.svc/GetCust", 
    contentType: "application/json", 
    dataType: "json", 
    data: "{'zzz': '" + $("#ctl00_zz_zzz").val() + "'}", 
    success: function(msg) { 
     // do stuff 
    }, 
    error: function(z) { alert(z.responseText); } 
}); 

回答

0

問題在於https。從中調用服務的頁面是https,因此「方法不允許」。

我改變了web.config和javascript文件來使用https調用服務,它工作。

This幫我一把。

現在我明白了爲什麼人們說WCF更多的是在前面工作。

我安裝了Windows Communication Foundation HTTP激活和非HTTP激活,這與我的問題無關,但我仍然不清楚它們是什麼。

2

請嘗試將以下屬性放在類別Cust

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
+0

我有這在web.config中,它沒有工作。我確實把上面的代碼隱藏起來,但沒有運氣。 – Steve 2010-06-16 21:35:53

相關問題