對此問題堅持不懈。我有一個解決方案與2個項目,其中之一是一個簡單的舊jQuery jQuery Ajax調用,而另一個是WCF服務。該html頁面將發出一個ajax調用WCF服務來獲取一個json字符串並將其用於顯示目的。WCF錯誤:405方法不允許
現在的問題是,每當我運行在調試模式下,html頁面和WCF都將以不同的端口啓動。當我執行測試時(例如,在Firefox中調用type = OPTIONS時,得到405方法不允許錯誤),這就爲我創建了一個跨源問題。我會三重檢查我的ajax腳本的調用方法,並且WCF服務是相同的(GET)。
我會搜索谷歌,但發現無論我必須安裝擴展或在IIS上執行一些配置,我發現很麻煩,因爲我正在做的事情很簡單。下面的一個例子,我要補充在我的web.config以下的配置,但它沒有工作:
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="crossDomain" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="MobileService.webHttpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="MobileService.SimpleMemberInfo" behaviorConfiguration="MyServiceBehavior">
<endpoint address="" binding="webHttpBinding" contract="MobileService.IMemberInfo" bindingConfiguration="crossDomain" behaviorConfiguration="MobileService.webHttpBehavior">
</endpoint>
</service>
</services>
</system.serviceModel>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
</customHeaders>
</httpProtocol>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
任何一個已經擺脫這惱人的問題的任何想法?
編輯:我想補充,我正在與IIS快遞,與VS工作室2012
一起來到添加在WCF代碼的調試和更新的web.config
[ServiceContract]
public interface IMemberInfo
{
[WebInvoke(Method = "GET",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json
)]
[OperationContract]
string GetMemberInfoById();
// TODO: Add your service operations here
}
我的腳本:
$(document).ready(function() {
$.ajax("http://localhost:32972/SimpleMemberInfo.svc/GetMemberInfoById", {
cache: false,
beforeSend: function (xhr) {
$.mobile.showPageLoadingMsg();
},
complete: function() {
$.mobile.hidePageLoadingMsg();
},
contentType: 'application/json',
dataType: 'json',
type: 'GET',
error: function() {
alert('Something awful happened');
},
success: function (data) {
var s = "";
s += "<li>" + data + "</li>";
$("#myList").html(s);
}
});
});
此鏈接可能會幫助http://stackoverflow.com/questions/2202500/wcf-service-405-method-not-allowed-exception – Mihai
感謝。我試過了,但沒有奏效。 – ipohfly
ops ok ...但如果沒有一個答案真的有幫助,那麼我應該選擇最接近的一個? – ipohfly