【发布时间】: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