这一信息非常明确:
“access control allow origin”的值
响应中的头
不能是通配符“*”
当请求的凭据模式为
'包括
发生这种情况是因为您正在设置属性
withCredentials
关于你的
XMLHttpRequest
到
true
. 所以你需要去掉通配符,然后添加
Access-Control-Allow-Credentials
头球。
res.header("Access-Control-Allow-Origin", "http://localhost:4200");
res.header('Access-Control-Allow-Credentials', true);
你可以用
cors
包,以便轻松实现白名单:
const cors = require('cors');
const whitelist = ['http://localhost:4200', 'http://example2.com'];
const corsOptions = {
credentials: true, // This is important.
origin: (origin, callback) => {
if(whitelist.includes(origin))
return callback(null, true)
callback(new Error('Not allowed by CORS'));
}
}
app.use(cors(corsOptions));