仅 Chrome 中的无效 HTTP 状态代码 405

Invalid HTTP status code 405 in Chrome only

我有一个将客户保存到数据库的 WCF 服务,在 Internet Explorer 中按预期工作,但在 [=31] 中 =](出于这个原因,我将省略 HTML/Json,因为它让我认为这是一个网络配置问题,但如果有人愿意,我可以 post)。

在 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,但似乎没有什么不同。

好吧,我本来打算删除这个帖子的,但我想以后可能会有人遇到这个问题,以免他们头疼。

在我的网络配置中,我删除了(否则你会得到一个错误)

<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

希望对您有所帮助

计算机的答案适用于我的 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();
}