【发布时间】:2019-08-11 08:01:21
【问题描述】:
API 是 AspNetCore WebApi,默认配置为 Windows 身份验证并启用了 CORS。客户端是带有 GET 和 POST 方法的 Angular。
GET调用成功:
this.http.get("https://localhost:44358/api/values", {withCredentials:true})
.subscribe(a=> {
console.log(a);
this.list=a;
});
POST 失败:
this.http.post("https://localhost:44358/api/values", {value:"aaa"}, {withCredentials:true})
.subscribe(a=> {
console.log(a);
this.list=a;
});
02个例外是
OPTIONS https://localhost:44358/api/values 401 (Unauthorized)
和
Access to XMLHttpRequest at 'https://localhost:44358/api/values' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
有什么想法吗?
【问题讨论】:
-
这是 CORS 的问题,而不是 Angular 的问题。错误很明显,您需要在后端实现“Access-Control-Allow-Origin”标头。在您的情况下,CORS 的原因是应用程序的不同端口(客户端和服务器)。 PS。确保您允许 OPTIONS 请求,这是使 CORS 工作所必需的。
标签: angular cors asp.net-core-webapi windows-authentication