我有一个WCF服务,可以将客户保存到数据库中,在Internet Explorer中按预期工作但在Chrome中不(因此我将省略HTML / Json,因为它让我觉得这是一个网络配置问题,但我可以发帖,如果有人喜欢)。
在Chrome中我收到错误:
无法加载资源:服务器响应状态为405(方法不允许) Subscriber.htm:1 XMLHttpRequest无法加载http://example.com/MyService.svc/SaveCustomer。无效的HTTP状态代码405
我的网站配置:
<system.web>
<compilation debug="false" strict="false" explicit="true" targetFramework="4.0" />
<pages>
<namespaces>
<add namespace="System.Runtime.Serialization" />
<add namespace="System.ServiceModel" />
<add namespace="System.ServiceModel.Web" />
</namespaces>
</pages>
</system.web>
<system.serviceModel>
<services>
<service name="IService.MyService">
<endpoint address="http://example.com/MyService.svc"
binding="webHttpBinding"
bindingConfiguration=""
contract="IService"
behaviorConfiguration="webHttpBinding" />
</service>
</services>
<client>
<endpoint
binding="webHttpBinding"
bindingConfiguration=""
address="http://example.com/MyService.svc"
contract="IService" />
</client>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttpBinding">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<httpProtocol>
<customHeaders>
<!-- Enable Cross Domain AJAX calls -->
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Authorization, Accept, X-Requested-With" />
<add name="Access-Control-Allow-Methods" value="OPTIONS, TRACE, GET, HEAD, POST, PUT" />
</customHeaders>
</httpProtocol>
</system.webServer>
所以我已经阅读了一些文章并添加了相关部分,删除了WebDav,但似乎没有任何区别。
答案 0 :(得分:13)
是的,我打算删除这个帖子但是认为将来有人可能会遇到这个问题并且让他们免于头疼。
在我的webconfig中,我已删除(否则会出错)
<httpProtocol>
<customHeaders>
<!-- Enable Cross Domain AJAX calls -->
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Authorization, Accept, X-Requested-With" />
<add name="Access-Control-Allow-Methods" value="OPTIONS, TRACE, GET, HEAD, POST, PUT" />
</customHeaders>
</httpProtocol>
在我的WCF Web服务中,我添加了一个新的Global.asax并添加了
Sub Application_BeginRequest(sender As Object, e As EventArgs)
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*")
If HttpContext.Current.Request.HttpMethod = "OPTIONS" Then
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache")
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST")
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept")
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000")
HttpContext.Current.Response.End()
End If
End Sub
希望这有帮助
答案 1 :(得分:3)
计算机的答案适用于我的ASP.NET MVC 5站点,其他解决方案失败了。
这是他在C#中的Global.asax功能。
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod != "OPTIONS") return;
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}