Javascript HttpOnly cookie不是使用axios从下一个js getServerSideProps发送的(带凭据:true)
这就是我的问题。我有一个dasboard页面和程序页面问题出在我有SSR的程序页面上,因为在仪表板页面上,我在客户端调用了我的传奇,一切都正常工作 客户端:客户端将httpOnly cookie发送到我的后端服务器,并从我的后端获取数据以供使用 服务器端:但是,出于某种原因,当我调用getServerSideProps中的相同传奇时,当然{withCredentials:true}出于某种原因,它不会将令牌发送到我的后端。在从getServerSideProps获得的req对象中,我在req.headers.cookie中看到了cookie,但它没有发送它。那么,在从getServerSideProps或getServerSideProps调用时手动添加它的解决方案是什么呢 守则:Javascript HttpOnly cookie不是使用axios从下一个js getServerSideProps发送的(带凭据:true),javascript,reactjs,express,axios,next.js,Javascript,Reactjs,Express,Axios,Next.js,这就是我的问题。我有一个dasboard页面和程序页面问题出在我有SSR的程序页面上,因为在仪表板页面上,我在客户端调用了我的传奇,一切都正常工作 客户端:客户端将httpOnly cookie发送到我的后端服务器,并从我的后端获取数据以供使用 服务器端:但是,出于某种原因,当我调用getServerSideProps中的相同传奇时,当然{withCredentials:true}出于某种原因,它不会将令牌发送到我的后端。在从getServerSideProps获得的req对象中,我在req.h
export const getServerSideProps = wrapper.getServerSideProps(
async ({ store, req }) => {
store.dispatch(fetchprogramsRequest(req.url));
const cookies = cookie.parse(req.headers.cookie);
console.log('COOKIES', cookies); // HERE you can see the cookies
// end the saga
store.dispatch(END);
await store.sagaTask.toPromise();
}
);
The axios inside the saga:
const res = yield axios.get(url, { withCredentials: true });
This is called in both cases (client-side: works, server-side: doesn't)
我相信cookie存储在客户端(浏览器) 也许这是可行的,只要你能让饼干成为传奇
// The axios inside the saga:
const res = yield axios.get(url, {
headers: {
Cookie: "cookie1=value; cookie2=value; cookie3=value;"
});
另一个选项是,如果您使用的是访问令牌,则可以像使用授权头一样发送它
const res = await axios.get(url, {
headers: { Authorization: `Bearer ${token}` },
});
是的。我能想到的唯一方法也是。。