我处于一种奇怪的情况下,浏览器中没有设置cookie,而浏览器中却显示了cookie

来自网络选项卡的响应屏幕截图

React应用程序在域上运行- https://2367cc15b.eu.ngrok.io
域上运行的节点Js - https://e17b14c2835b.ngrok.io
设置cookie的代码
res.cookie('holofyKey', holofyKey, { httpOnly: true, domain: '.ngrok.io', expires: new Date(new Date().getTime() + (1000 * 60 * 60 * 24 * 365)) });我在我的中间件中使用app.use(cookieParser());。
我是不是漏掉了什么?
PS -我尝试从选项中删除httpOnly和域名仍然没有成功
发布于 2021-07-23 16:16:14
在尝试了两天所有可能的解决方案后,这终于对我起作用了
这就是您需要调用要从中设置cookie的api的方式。
const postHelper = async (url, body) => {
return await fetch(url, {
method: "POST",
headers: {
Accept: "applicaiton/json",
"Content-Type": "application/json",
},
body: body && JSON.stringify(body),
withCredentials: true, // should be there
credentials: 'include' // should be there
});
};添加此代码后,您将收到CORS错误,因此请在您的服务器中添加此行代码
app.use(cors({ origin: true, credentials: true }));最后
res.cookie('cookieKey', cookieKey, { expires: new Date(new Date().getTime() + (1000 * 60 * 60 * 24 * 365)), secure: true });PS -此解决方案将在跨域和相同域的情况下工作,但在跨域的情况下,大多数浏览器将不允许您设置cookie,直到用户同意。
https://stackoverflow.com/questions/68468778
复制相似问题