【问题标题】:Why I'm having CORS error when calling an API为什么调用 API 时出现 CORS 错误
【发布时间】:2020-06-17 16:41:34
【问题描述】:

我在前端使用Angular,在后端使用C#,当我尝试编辑字段并保存时出现此错误:

Access to XMLHttpRequest at 'http://192.168.220.14:81/api/registry' from origin 'http://artc.tj' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

PUT http://192.168.220.14:81/api/reg polyfills-es2015.23e79c711e3c227d781f.js:1 PUT http://192.168.220.14:81/api/registry net::ERR_FAILED

Error Scrinshot

Startup.cs:

 public void ConfigureServices(IServiceCollection services)
 {
     //CORS
     services.AddCors(options =>
     {
         options.AddPolicy(MyAllowSpecificOrigins,
         builder =>
         {
             builder.WithOrigins("http://localhost",
                                 "http://localhost:4200",
                                 "http://artc.tj",
                                 "http://192.168.220.14")
                                    .AllowAnyMethod()
                                    .AllowAnyHeader()
                                    .AllowCredentials();
         });
     });
  }

【问题讨论】:

标签: angular asp.net-core cors


【解决方案1】:

确保在 Configure 方法中使用 Cors()

Startup.cs

public class Startup
{
   //...
   readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";

   public void ConfigureServices(IServiceCollection services)
   {
      //...
         services.AddCors(options =>
         {
             options.AddPolicy(MyAllowSpecificOrigins,
             builder =>
             {
                 builder.WithOrigins("http://localhost:4200")
                     .AllowAnyHeader()
                     .AllowAnyMethod()
                     .AllowAnyOrigin();
             });
         });
      //...
   }

   public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
   {
      //...
      app.UseCors(MyAllowSpecificOrigins);
      //...
   }
   //...
}

附:还要确保在您的客户请求中包含withCredentials: false httpOptions

【讨论】:

    【解决方案2】:

    我认为您在从Angular 端发送请求时缺少标头。这是我通常做的:

    Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
         services.AddCors(options =>
         {
             options.AddPolicy("Default", policy =>
             {
                 policy.AllowAnyOrigin()
                       .AllowAnyHeader()
                       .AllowAnyMethod();
             });
         });
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseCors("Default");
    }
    

    角度请求:

    callAPI() {
       return this.httpClient
                .get(API_URL, {headers: {'Access-Control-Allow-Origin' : '*'}});
    }
    

    【讨论】:

    • 客户端不决定服务器授予谁访问权限。 Access-Control-Allow-Origin 是服务器发送的响应头
    【解决方案3】:

    你没有。您向其发出请求的服务器必须实施 CORS 以授予 JavaScript 从您的网站访问权限。您的 JavaScript 无法授予自己访问其他网站的权限。

    使用来自 chrome 浏览器网络商店的 CORS 扩展。

    希望这可能会改变你的看法

    【讨论】:

      猜你喜欢
      • 2016-06-10
      • 2020-01-21
      • 2017-10-17
      • 2020-06-22
      • 2021-08-30
      • 2021-07-20
      • 2019-07-05
      • 2017-06-03
      • 1970-01-01
      相关资源
      最近更新 更多