简体   繁体   English

CORS包含从本地PC上的角度应用程序到asp.net核心API的帖子

[英]CORS with posts from angular app to asp.net core API on local pc

I have a local running ASP.NET core API service and a local Angular App. 我有一个本地运行的ASP.NET核心API服务和一个本地Angular App。 Most of my call's goes fine to the API but a single call keep giving me CORS errors. 我的大多数调用都可以使用该API,但是一次调用却不断给我带来CORS错误。 I suspect it is because it is an post operation? 我怀疑是因为这是后期操作? Does posts requires anything different according to CORS? 根据CORS,帖子要求的内容有所不同吗?

Anuglar ts: 丑角ts:

loadGroup(groupName:string){
    this.apiService.getInfluencersWithFilter(this.query,groupName,null,null,this.ageRanges).subscribe((data) => {     
      this.influencerSearchResult = data.results;
      // this.searchGroupsFacet = data.facetGroupList;
      this.searchSubGroupsFacet = data.facetSubGroupList;
      this.showSubGroups = true;

   });
  }

Angular service: 角度服务:

getInfluencersWithFilter(q:string, group:string, subGroups:string[], socialAccounts:string[],ageRanges:AgeRange[]):Observable<InfluencerSearchContainer>
{    
        if(q==null)
        {
          q = "";
        }
        var url = `${environment.apiDomain}/api/InfluencersSearch/`;
        return  this.httpClient.post<InfluencerSearchContainer>(url,{q:"",group:group,subGroups:subGroups, socialAccounts:socialAccounts, ageRanges:ageRanges}).pipe(
          map(x => new InfluencerSearchContainer(x)));      
 }

Startup ASP.NET core: 启动ASP.NET核心:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddScoped<DocumentClient>((s) =>
        {
            string EndpointUrl = Configuration["CosmosDB:EndpointUrl"];
            string PrimaryKey = Configuration["CosmosDB:PrimaryKey"];
            return new DocumentClient(new Uri(EndpointUrl), PrimaryKey);
        });

        var connStr = Configuration.GetConnectionString("DefaultConnection");
        services.AddDbContext<DB>(options => options.UseSqlServer(connStr));

        services.AddCors(options =>
        {
            options.AddPolicy("AllowSpecificOrigin",
                builder => builder.AllowAnyOrigin()
                       .AllowAnyHeader()
                       .AllowAnyMethod());
        });

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        app.UseCors("AllowSpecificOrigin");
        app.UseHttpsRedirection();
        app.UseMvc();
    }

And controller: 和控制器:

  [HttpPost]
    public InfluencerSearchResultWithFacets Post([FromBody] InfluencerQuery q)
    {
        return GetSearchResult(q.q, q.group,q.subGroups, q.socialAccounts, q.ageRanges);
    }

Is there anything I am missing? 我有什么想念的吗? I think everything is disabled here? 我认为这里一切都被禁用了吗? As I write I suspect it has something todo with the Post, because the get actions works. 在我撰写本文时,我怀疑它与Post有关,因为get操作有效。

In postman it works: 在邮递员中,它的工作原理是: 在此处输入图片说明

I have also added following on the controller: [EnableCors("AllowSpecificOrigin")] 我还在控制器上添加了以下内容:[EnableCors(“ AllowSpecificOrigin”)]

Sometimes, 有时,

1) If there is an error in the request payload (or) error in the api, the chrome developer tools console shows CORS. 1)如果api中的请求有效载荷(或)错误,则chrome开发人员工具控制台会显示CORS。

Please console.log the request and test the api separately with postman/swagger with that. 请console.log请求并使用postman / swagger分别测试api。

If 1) not solving the issue 如果1)无法解决问题

2) Sometimes IIS can block PUT and POST operations by default. 2)默认情况下,有时IIS可以阻止PUT和POST操作。 Please see this . 请看这个

if 1) and 2) not solving the problem 如果1)和2)没有解决问题

This could also be a problem 这也可能是一个问题

3) We May have to add [EnableCors("AllowSpecificOrigin")] to the controller. 3)我们可能必须向控制器添加[EnableCors(“ AllowSpecificOrigin”)]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM