在我的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 核心 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