0

I have a local running ASP.NET core API service and a local Angular App. Most of my call's goes fine to the API but a single call keep giving me CORS errors. I suspect it is because it is an post operation? Does posts requires anything different according to CORS?

Anuglar 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:

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.

In postman it works: enter image description here

I have also added following on the controller: [EnableCors("AllowSpecificOrigin")]

Thomas Segato
  • 3,137
  • 3
  • 33
  • 63
  • There may be some other error getting thrown and the API is returning a CORS errors when it really isn't a CORS error. You should debug the API when making the API call to see if something is going on there. – R. Richards Jan 19 '19 at 14:04

1 Answers1

1

Sometimes,

1) If there is an error in the request payload (or) error in the api, the chrome developer tools console shows CORS.

Please console.log the request and test the api separately with postman/swagger with that.

If 1) not solving the issue

2) Sometimes IIS can block PUT and POST operations by default. Please see this .

if 1) and 2) not solving the problem

This could also be a problem

3) We May have to add [EnableCors("AllowSpecificOrigin")] to the controller.

staticvoidmain
  • 753
  • 6
  • 13
  • Thanks, I have tried postman and had the attribute on :( I will look into bullet 2. – Thomas Segato Jan 19 '19 at 14:24
  • if postman works, IIS allows POST. Point 2) is working. I doubt the UI service. – staticvoidmain Jan 19 '19 at 14:31
  • Makes sense about 2). So you think its the angular part? I will have a look in fiddler. – Thomas Segato Jan 19 '19 at 14:37
  • You were somehow right. Fiddler showed it had nothing to do with CORS I got following error: FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. The system cannot find the file specified. I updated the Newton nuget package and following it worked. I dont understand postman worked, but the angular didnt, thats really wierd. But it works now that is whats count. Thanks both of you highly appreciated. – Thomas Segato Jan 19 '19 at 14:46