Asp.net core 已被CORS策略阻止--为什么我在asp.net core中收到post请求的此错误?

Asp.net core 已被CORS策略阻止--为什么我在asp.net core中收到post请求的此错误?,asp.net-core,asp.net-web-api,Asp.net Core,Asp.net Web Api,对于GET请求,我得到了相同的错误,并且我修复了这个错误,但是现在对于POST请求,我再次得到了这个错误。对于GET请求,我通过将其写入startup.cs文件修复了该问题。我能做什么 public void ConfigureServices(IServiceCollection services) { services.AddCors(options => { options.AddPolicy(name: MyAllowSpecificOrigins, bui

对于
GET
请求,我得到了相同的错误,并且我修复了这个错误,但是现在对于
POST
请求,我再次得到了这个错误。对于
GET
请求,我通过将其写入startup.cs文件修复了该问题。我能做什么

public void ConfigureServices(IServiceCollection services) {
  services.AddCors(options => {
    options.AddPolicy(name: MyAllowSpecificOrigins,
      builder => {
        builder.WithOrigins("https://localhost:fhfhhfh",
          "https://localhost:fhfhf");
      });
  });

  services.AddControllers();
  services.AddRazorPages();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, GameUserContext db) {
  if (env.IsDevelopment()) {
    app.UseDeveloperExceptionPage();
  }

  db.Database.EnsureCreated();
  app.UseHttpsRedirection();
  app.UseStaticFiles();
  app.UseRouting();

  app.UseCors();

  app.UseAuthorization();
  app.UseCors(MyAllowSpecificOrigins);


  app.UseEndpoints(endpoints => {
    endpoints.MapControllers();
    endpoints.MapRazorPages();

  });
}

通过如下方式扩展生成器,允许POST方法:

builder.WithOrigins("https://localhost:fhfhhfh", "https://localhost:fhfhf")
       .WithMethods("POST", "GET");

或者您可以允许任何方法:

builder.WithOrigins("https://localhost:fhfhhfh", "https://localhost:fhfhf")
       .AllowAnyMethod();
将app.UseCors()更改为app.UseCors(MyAllowSpecificCorigins)

如果它仍然不起作用

尝试将生成器更改为builder=>{ 建筑商。来源(“https://localhost:fhfhhfh", "https://localhost:fhfhf") .AllowAnyHeader() .AllowAnyMethod();
});

请您发布您现在遇到的详细错误消息,好吗?您向应用程序发送了哪个请求?仍不工作。在XMLHttpRequest.l的e.exports(spread.js:25)上也出现了这个错误spread.js:25 Uncaught(in promise)error:networkerror。onerror@IsratJerin现在,若要查看是否是cors问题,您可以将
WithOrigins
方法更改为
AllowAnyOrigin()
,还可以使用
AllowAnyMethod()
并添加
AllowAnyHeader()
你能帮帮我吗?谢谢,伙计,这很有效。但仍在控制台中给出错误消息,我需要重新加载以查看每个请求的结果:(从源站“”访问“”处的XMLHttpRequest已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许源站”标头。spread.js:25 Uncaught(承诺中)错误:e.exports(spread.js:25)处的网络错误XMLHttpRequest.l.onerror(spread.js:25)这是用于axios cdn的吗?我添加了这个cdn检查,如果添加“.AllowCredentials();”之后的“AllowAnyMethod()”将起作用,我会在上面的代码中看到您正在使用app.UserCors两次。请仅使用app.UseCors(MyAllowSpecificationCorigins);还有一件事-您是否尝试使用builder.AllowAnyOrigin()而不是url列表?