Invalid HTTP status code 405 in Chrome only

巧了我就是萌 提交于 2019-12-05 09:04:30

Right, i was going to delete this thread but thought that someone may come across this issue in future and to save them from a headache.

In my webconfig, i removed (otherwise you get an error)

<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>

In my WCF web service i added a new Global.asax and added

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

Hope this helps

Computer's answer worked for my ASP.NET MVC 5 site where other solutions failed.

Here's his Global.asax function in C#.

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!