我正在使用Angular应用程序,但是当我调用本地API时出现此错误
无法加载https://localhost:44358/api/try:所请求的资源上不存在“ Access-Control-Allow-Origin”标头。因此,不允许访问来源“ http://localhost:4200”。
我也有这个:
错误 HttpErrorResponse {标头:HttpHeaders,状态:0,statusText:“未知错误”,网址:null,确定:false,...} 错误 : ProgressEvent
所以我看到了一些解决方案,并在启动文件中的.Net Core API上尝试了此操作
services.AddCors(options =>
{
options.AddPolicy("AnyOrigin", builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
但是我有同样的错误... 我可以帮忙吗?
最后它成功了,我想我忘了构建.Net Core API
答案 0 :(得分:1)
如果使用Angular 6,Angular CLI支持代理配置,它将代理您的后端API调用。请检查文档。 https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md