我正在开发 Angular 应用程序,但是当我调用本地 API 时出现此错误
Failed to load https://localhost:44358/api/try: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
我还有这个:
ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, …} error : ProgressEvent
所以我看到了一些解决方案,并在启动文件中的 .Net Core API 上尝试了这个
services.AddCors(options =>
{
options.AddPolicy("AnyOrigin", builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
但是我有同样的错误... 我能帮忙吗?
终于成功了,我想我忘了构建我的 .Net Core API
最佳答案
如果您使用 Angular 6,Angular CLI 支持代理配置,它将代理您的后端 API 调用。请检查文档。 https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md
关于c# - 无法加载 https ://localhost:<port>/api/try: No 'Access-Control-Allow-Origin' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51634756/