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")]

1
  • 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. Commented Jan 19, 2019 at 14:04

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I have tried postman and had the attribute on :( I will look into bullet 2.
if postman works, IIS allows POST. Point 2) is working. I doubt the UI service.
Makes sense about 2). So you think its the angular part? I will have a look in fiddler.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.