对此问题坚持不懈。我有一个解决方案与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