【问题标题】:Cross-Origin Request Blocked: Occur even after CORS enabled on my API ;跨域请求被阻止:即使在我的 API 上启用了 CORS 后也会发生;
【发布时间】:2019-04-29 15:06:08
【问题描述】:

在我的反应应用程序中,这将显示。当我尝试发出发布请求时。我在后端使用 .net core 2.2 webapi。

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5050/api/Users. (Reason: missing token ‘access-control-allow-origin’ in CORS header ‘Access-Control-Allow-Headers’ from CORS preflight channel).[Learn More]
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5050/api/Users. (Reason: CORS request did not succeed).[Learn More]

在我的 .net 核心 webapi 中。我也启用了cors。 使用IServiceCollection services

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

IApplicationBuilder app

app.UseCors("AllowAll");

我也在控制器中使用过这个。

[EnableCors("AllowAll")]

编辑:调试控制台输出。

    info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 OPTIONS http://localhost:5050/api/Users  
    Request starting HTTP/1.1 OPTIONS http://localhost:5050/api/Users  
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
    Executing endpoint '405 HTTP Method Not Supported'
Microsoft.AspNetCore.Routing.EndpointMiddleware:Information: Executing endpoint '405 HTTP Method Not Supported'
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
    Executed endpoint '405 HTTP Method Not Supported'
Microsoft.AspNetCore.Routing.EndpointMiddleware:Information: Executed endpoint '405 HTTP Method Not Supported'
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
    Request finished in 9.3884ms 405 
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 9.3884ms 405 

【问题讨论】:

  • 您是否在 IIS 上托管?
  • @Moby'sStuntDouble 我想是的。我正在为这个项目使用 VSCode 和 .net CLI。
  • 你是如何运行后端的?您是从命令行使用dotnet run 启动它,还是使用VSCode 进行调试?你的launchSettings.json 是什么样的?
  • @rickvdbosch 是的,我尝试两种方式。 launch.json 由 vscode 自动生成。

标签: javascript c# .net reactjs cors


【解决方案1】:

首先要检查的是这个错误消息是否不是 web api 错误的结果。

为此,请在特殊模式下运行 chrome (windows)

"c:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --user-data-dir="C:/Chrome 开发会话" --disable-web-security

如果您仍然收到错误,那么我怀疑这可能是由于您的 web api 中的一些其他错误。你检查过 api 控制台是否有异常吗?

干杯

【讨论】:

  • 它在这种特殊模式下在 chrome 上工作。你知道问题所在吗?
  • 缩小范围的好方法!
【解决方案2】:

如果您使用完整的 IIS 并遇到问题,请确保排除:

IIS > App or Directory \ HTTP Response Headers \ Access-Control-Allow-Origin

已经设置好了。我遇到了一个问题,由于某种原因它已经被设置并覆盖了应用程序的配置。

【讨论】:

    【解决方案3】:

    试试 Response.Headers.Add("Access-Control-Allow-Origin", "http://IPorDomain:Port");它适用于 .Net Core Web API。就我而言,我正在使用 Angular 的 Web api。

    var itemsOnPage = await _context.Employees
                .OrderBy(e => e.EmpFirstName)               
                .Skip(count * pageIndex)
                .Take(count)
                .ToListAsync();
    
    /*This*/
    Response.Headers.Add("Access-Control-Allow-Origin", "http://localhost:4200");
    /*This*/
    return Ok(itemsOnPage);
    

    【讨论】:

    • 还是一样。还是谢谢
    【解决方案4】:

    首先我要感谢所有试图帮助我的人。我已经解决了这个问题。

    首先我使用添加和删除程序卸载了IIS
    而且我还关闭了 Windows 功能 中的 IIS 功能。

    需要重启电脑。 之后,这个问题就不会发生了。

    【讨论】:

      猜你喜欢
      • 2021-03-29
      • 2015-05-05
      • 2018-06-13
      • 2021-10-28
      • 2020-08-16
      • 1970-01-01
      • 2020-05-06
      • 2019-01-30
      • 1970-01-01
      相关资源
      最近更新 更多