在我的应用中,这将显示。当我尝试发出发布请求时。我正在使用.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

最佳答案

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

为此,请以特殊模式运行Chrome(Windows)

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

如果仍然出现错误,那么我怀疑这可能是由于您的Web API中的其他错误所致。您是否检查过API控制台是否有任何异常?

干杯

09-30 17:19
查看更多