如何从保存在服务器服务上的客户端服务访问 cookie
How to access the cookie from client service that is saved on server service
其实我在reactjs上创建的项目有两个服务。第一个是本地主机端口 3000 上 运行 的客户端服务,第二个是端口 8000 上 运行 的服务器服务。我在服务器服务上创建了 cookie,并希望从客户端服务访问。但是 cookie 保存在服务器端口上。如何在客户端保存此cookie或从客户端访问cookie。
这是服务器服务上的代码
res.cookie("jwt",token,{expires:new Date(Date.now()+99999,
httpOnly: true
})
我使用 withCredentials 属性 修复了它。
来自不同域的 Axios 请求无法为其自己的域设置 cookie 值,除非在发出请求之前将 withCredentials 设置为 true。
axios.get('some api url', {withCredentials: true});
我还在 cors 中将 origin 和 credentials 设为 true
const corsOptions = {
origin: true, //included origin as true
credentials: true, //included credentials as true
};
app.use(cors(corsOptions));
现在对我来说效果很好。它向客户端服务器发送一个响应 cookie。
see here
其实我在reactjs上创建的项目有两个服务。第一个是本地主机端口 3000 上 运行 的客户端服务,第二个是端口 8000 上 运行 的服务器服务。我在服务器服务上创建了 cookie,并希望从客户端服务访问。但是 cookie 保存在服务器端口上。如何在客户端保存此cookie或从客户端访问cookie。
这是服务器服务上的代码
res.cookie("jwt",token,{expires:new Date(Date.now()+99999,
httpOnly: true
})
我使用 withCredentials 属性 修复了它。
来自不同域的 Axios 请求无法为其自己的域设置 cookie 值,除非在发出请求之前将 withCredentials 设置为 true。
axios.get('some api url', {withCredentials: true});
我还在 cors 中将 origin 和 credentials 设为 true
const corsOptions = {
origin: true, //included origin as true
credentials: true, //included credentials as true
};
app.use(cors(corsOptions));
现在对我来说效果很好。它向客户端服务器发送一个响应 cookie。 see here