Response to preflight request doesn't pass access control check

后端 未结 21 2225
别那么骄傲
别那么骄傲 2020-11-22 03:50

I\'m getting this error using ngResource to call a REST API on Amazon Web Services:

XMLHttpRequest cannot load http://server.apiurl.com:8000/s/login

21条回答
  •  悲&欢浪女
    2020-11-22 04:23

    In AspNetCore web api, this issue got fixed by adding "Microsoft.AspNetCore.Cors" (ver 1.1.1) and adding the below changes on Startup.cs.

    public void ConfigureServices(IServiceCollection services)
    { 
        services.AddCors(options =>
        {
              options.AddPolicy("AllowAllHeaders",
                    builder =>
                {
                        builder.AllowAnyOrigin()
                               .AllowAnyHeader()
                               .AllowAnyMethod();
                    });
        });
        .
        .
        .
    }
    

    and

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
    
    
        // Shows UseCors with named policy.
        app.UseCors("AllowAllHeaders");
        .
        .
        .
    }
    

    and putting [EnableCors("AllowAllHeaders")] on the controller.

提交回复
热议问题