【问题标题】:Response for preflight has invalid HTTP status code 405 on Angular Js1 and Webapi 2预检响应在 Angular Js1 和 Webapi 2 上具有无效的 HTTP 状态代码 405
【发布时间】:2018-01-04 09:22:42
【问题描述】:

错误:

OPTIONS http://localhost:8080//api/home/GetText 405 (Method Not Allowed)

http://localhost:8080//api/home/GetText. Response for preflight has invalid HTTP status code 405
angular.js:14362 Error: Error getting home/GetText data from the database! 
Headers:

响应标头:

Access-Control-Allow-Credentials:false
Access-Control-Allow-Headers:gid,Origin, X-Requested-With, Content-Type, Accept, Authorization
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Allow:GET
Content-Length:76
Content-Type:application/json; charset=utf-8
Date:Fri, 28 Jul 2017 10:30:06 GMT
Server:Microsoft-IIS/10.0
X-Powered-By:ASP.NET

请求标头:

Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:authorization,gid
Access-Control-Request-Method:GET
Cache-Control:no-cache
Connection:keep-alive
Host:localhost:8080
Origin:http://evil.com/
Pragma:no-cache
Referer:http://localhost:1852/
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36

请注意,我使用 Angular Js 作为客户端,使用 Webapi 作为服务器端。我正在通过 $resource 服务执行 xhr 请求。

【问题讨论】:

    标签: angularjs cors asp.net-web-api2 angular-resource preflight


    【解决方案1】:

    这是因为您的后端 API 不支持 OPTIONS 请求您的路由(特别是 /api/home/GetText 此路由)。 AngularJS 通常通过在发出实际内容请求之前发出 OPTIONS 请求来检查 CORS 请求。

    您需要在后端启用对 OPTIONS 请求的支持,并从那里返回适当的标头 + 良好的 HTTP 状态 (200-299)。

    【讨论】:

    • 我添加了相同的代码。现在我无法阅读标题。
    • 我想阅读标题“X-filename:Letter.pdf”。在开发人员模式下打开请求时获得响应,但在 js 文件中我没有得到它。在 IE 中它工作正常。
    【解决方案2】:
     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, PUT");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "gid, filename, Origin, X - Requested - With, Content - Type, Accept, Authorization");
                HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers", "X-filename");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
    

    这解决了上述问题。

    【讨论】:

      【解决方案3】:

      这意味着您正在尝试使用错误的 HTTP 动词从 FRONT END 到 BACK END 调用方法(例如 PUT INSTEAD of POST)

      仔细检查

      【讨论】:

      • 我在两端都做了一个get操作。我无法读取 chrome 中的自定义标题。但在 IE 中它工作正常
      • 您是否尝试过使用 OWIN CORS PACKAGE FOR WEBAPI 并执行 app.UseCors(CORS.AllowAll)
      • 是的,我试过了,但没有用。 app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
      • 您可以发布您的启动文件(qhere you put the allowcors )和 webconfig 吗?
      • 启动文件:link 配置:link
      猜你喜欢
      • 2016-10-10
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 2018-05-02
      • 1970-01-01
      • 2018-03-08
      • 2016-02-08
      • 2018-01-25
      相关资源
      最近更新 更多