我已经在这样的服务器上设置了
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
res.header(
'Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization, X-PINGOTHER'
);
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS');
next();
});
和客户端的 axios (react) 像这样
axios.defaults.withCredentials = true;
axios('http://127.0.0.1:3001/orders', {
method: 'GET',
withCredentials: true
}).then(res => {
console.log(res);
}).catch(err => {
console.log(err.response);
})
当我用 Postman 测试并直接输入 chrome 时,一切正常。知道我的代码有什么问题吗?