在我的 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/