【发布时间】:2020-10-04 21:47:33
【问题描述】:
我正在尝试编写一个 agular 应用程序,它将向 ASP.NET Api 发出一些 http 请求。
这是我来自 Angular 的请求(托管在 http://localhost:4200):
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
})
};
let body = {
"email": this.temail,
"password": btoa(this.tepassword)
}
this.http.post<any>(environment.authUrl, body, this.httpOptions).subscribe(
(data) => console.log(data),
(err) => console.log(err)
)
这是我在 ASP .Net Api 上的简单方法(监听 https://localhost:44317/):
[HttpPost]
public HttpResponseMessage Auth([FromBody] User user)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
我已经安装了 CORS nuget 并在配置文件中使用它:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.EnableCors();
我还为 Chrome 安装了 Allow CORS: Access-Control-Allow-Origin 扩展( 虽然我使用 Edge 铬)。
每次我尝试从 Angular 向 ASP 发送 POST 请求时都会收到此错误:
Access to XMLHttpRequest at 'https://localhost:44317/api/user/' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
有什么建议吗?
【问题讨论】:
-
在启动文件中配置cors的时候还需要配置domains to allow under,看这里。 docs.microsoft.com/en-us/aspnet/core/security/…