vue訪問.netCore api後臺跨域問題
報錯信息
:8080/#/Index:1 Access to XMLHttpRequest at ‘http://localhost:5000/api/values/5’ from origin ‘http://localhost:8080’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource
解決辦法:
後臺代碼中在startup.cs中ConfigureServices下配置以下代碼
//配置跨域处理
services.AddCors(options =>
{
options.AddPolicy("any", builder =>
{
builder.AllowAnyOrigin() //允许任何来源的主机访问
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();//指定处理cookie
});
});
controller上加一行注解
[EnableCors(“any”)]