简体   繁体   English

Net 2.1,Angular 7,被CORS策略阻止:在请求的请求中不存在“ Access-Control-Allow-Origin”标头

[英]Net 2.1, Angular 7, blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested

I am getting blocked by CORS policy. 我被CORS政策封锁。 I have allowed access to all in my startup.cs This is my startup.cs 我已经在我的startup.cs中允许访问所有文件。这是我的startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors();

        services.AddDbContext<Models.StockContext>(opt => opt.UseInMemoryDatabase("item"));

        app.UseCors(builder =>
        {
            builder
            .AllowAnyOrigin()
            .AllowAnyMethod()
            .AllowCredentials()
            .AllowAnyHeader();
        });

just add this lines in your ConfigureServices part and it should work fine: 只需在您的ConfigureServices部分中添加以下行,即可正常运行:

var corsBuilder = new CorsPolicyBuilder();
        corsBuilder.AllowAnyHeader();
        corsBuilder.WithMethods("GET", "POST");
        corsBuilder.AllowAnyOrigin();
        services.AddCors(options => options.AddPolicy("AllowAll",corsBuilder.Build()));

Please follow the documentation . 请遵循文档

First, you need to enable CORS middleware inside ConfigureServices() Second, you need to tell application to use this middleware inside Configure() 首先,您需要在ConfigureServices()启用CORS中间件,其次,您需要告诉应用程序在Configure()使用此中间件。

Example: 例:

In ConfigureServices() : ConfigureServices()

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

In Configure() : Configure()

    app.UseCors("AllowAllOrigins");

暂无
暂无

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

相关问题 AWS - 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”header - AWS - has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource 对 XMLHttpRequest 的访问已被 CORS 策略阻止 请求的资源上不存在“Access-Control-Allow-Origin”标头 - react Access to XMLHttpRequest has been blocked by CORS policy No 'Access-Control-Allow-Origin' header is present on the requested resource .NET Core 2.0中的CORS“请求的资源上没有&#39;Access-Control-Allow-Origin&#39;标头。” - CORS in .NET Core 2.0 “No 'Access-Control-Allow-Origin' header is present on the requested resource.” 在Asp.net Core 2.1的Angular 6中不存在“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present Angular 6 with Asp.net Core 2.1 c#已启用CORS的Web Api和所请求资源上存在可怕的“Access-Control-Allow-Origin”标头 - c# Web Api with CORS Enabled and the dreaded No 'Access-Control-Allow-Origin' header is present on the requested resource CORS错误-所请求的资源上没有“ Access-Control-Allow-Origin”标头 - CORS error - No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource ASP.NET Web窗体:所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - ASP.NET Web Forms: No 'Access-Control-Allow-Origin' header is present on the requested resource CORS 策略已阻止从 *** 从源 *** 获取访问权限:无“访问控制允许源” - Access to fetch at *** from origin *** has been blocked by CORS policy: No 'Access-Control-Allow-Origin' 跨域请求被阻止:&原因:CORS标头“ Access-Control-Allow-Origin”丢失 - Cross-Origin Request Blocked: & Reason: CORS header 'Access-Control-Allow-Origin' missing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM