.net core 的跨域报错“has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.”

webapi单独项目,vue的项目需要调用webapi的接口,出现:

“has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.”

需要修改.net core webapi的Program.cs的跨域策略:

region 跨域

builder.Services.AddCors(cor =>
{
cor.AddPolicy(“Cors”, policy =>
{
policy
//.WithOrigins(“https://localhost:15911”, “http://0.0.0.0:3201”)// 允许部分站点跨域请求
.AllowAnyOrigin()// 允许所有站点跨域请求
.AllowAnyHeader()// 允许所有请求头
//.AllowCredentials() // 允许Cookie信息,这句中途出过问题删了就好了,没查出原因
.AllowAnyMethod();// 允许所有请求方法
});
});

endregion

app.UseHttpsRedirection();
// 设置跨域
app.UseCors(“Cors”);

app.UseAuthorization();

app.MapControllers();

app.Run();

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏