javascript - 跨源请求被阻止 : Occur even after CORS enabled on my API ;

标签 javascript c# .net reactjs cors

在我的 react 应用程序中这将显示。当我尝试发出帖子请求时。我使用 .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 core webapi 中。我也启用了cors。 使用IServiceCollection服务

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

IApplicationBuilder应用

app.UseCors("AllowAll");

我也在 Controller 中使用了它。

[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 

最佳答案

首先要检查的是此错误消息是否不是 Web API 中错误的结果。

要执行此操作,请在特殊模式下运行 chrome(Windows)

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

如果您仍然收到错误,那么我怀疑这可能是由于您的 Web api 中的其他错误造成的。您是否检查过 api 控制台是否有任何异常?

干杯

关于javascript - 跨源请求被阻止 : Occur even after CORS enabled on my API ;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53507051/

相关文章:

javascript - 在 Observables 中操作数据的模式是什么(Angular2)

javascript - jQuery 不解析 HTML

javascript - 在点击事件/函数期间获取 URL 哈希值

c# - ASP.NET WebAPI 中 HttpServiceHost 的等价物是什么?

c# - C#和.NET中P/Invoking时的特性检测

javascript - 按键合并对象并添加值Javascript

c# - 如果 IF 语句不满足第一个条件,它会停止评估吗?

.net - 更改已部署类库的应用程序设置?

c++ - 密码策略 Windows API

c# - 如何将新操作分配给现有方法?