Angularjs 飞行前的响应在Js1和Webapi 2上具有无效的HTTP状态代码405

Angularjs 飞行前的响应在Js1和Webapi 2上具有无效的HTTP状态代码405,angularjs,cors,asp.net-web-api2,angular-resource,preflight,Angularjs,Cors,Asp.net Web Api2,Angular Resource,Preflight,错误: 响应标题: 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 data

错误:

响应标题:

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

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

这意味着您试图用错误的HTTP谓词从前端到后端调用一个方法(例如PUT而不是POST)


仔细检查它

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

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

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

这解决了上述问题。

我添加了相同的代码。现在我无法读取标题。我想读取标题“X-filename:Letter.pdf”。在开发人员模式下打开请求时得到响应,但在js文件中我没有得到响应。在IE中,它工作得很好。我正在两端做get操作。我无法在chrome中读取自定义标题。但在IE中,它工作得很好。你是否尝试过在WEBAPI和do应用程序中使用OWIN CORS包。UseCors(CORS.AllowAll)是的,我尝试过,但没有工作。app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);你能发布你的启动文件(QQ你把allowcors放在这里)和网络配置吗?启动文件:配置:
 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();
        }