c# - 无法加载 https ://localhost:<port>/api/try: No 'Access-Control-Allow-Origin'

标签 c# angular http asp.net-core

我正在开发 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/

相关文章:

java - 无法提取隐写术中的文本

c# - 匹配 Url 的正则表达式,特定域除外

safari - Angular2 Safari 后退按钮

Angular 2/4组件单元测试点击事件不触发变化

javascript - 获取 jQuery 后重定向响应

java - 具有相同RestEndPoint的两种不同方法:如何解决?

c# - 如何在 asp.net core 中使用 Fetch api 将数据传递给 Controller

javascript - Angular 4 日期管道短时间格式,每分钟更新一次

javascript - 如何在特定的 AJAX 请求中强制使用 HTTP/1.1

c# - 具有不同方向的 WPF 虚拟化 TreeView 不虚拟化?