【发布时间】:2019-03-31 20:27:37
【问题描述】:
我已经在这样的服务器上设置了
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 时,一切正常。知道我的代码有什么问题吗?
【问题讨论】:
-
您是在设置 cookie 服务器端还是客户端?您需要确保未使用 http-only 标志设置 cookie。如果您打开 chrome 开发人员工具并转到“应用程序”然后转到“Cookies”并检查 HTTP 列中是否有复选 (✓) 图标,则可以检查他们是否有此标志。
-
@RalphNajm 哦,实际上我的错误是将其设置为不仅仅是 http。谢谢,现在可以使用了
-
欢迎您:)
标签: node.js reactjs cookies cors axios