- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以这是我的问题。我有一个仪表板页面,我有程序页面。 问题出在我拥有 SSR 的程序页面上,因为在仪表板页面上,我在客户端调用了我的传奇,并且一切正常。
客户端:客户端将 httpOnly cookie 发送到我的后端服务器,并从我的后端获取数据以供使用。
服务器端 :但是由于某种原因,当我在 getServerSideProps 中调用相同的传奇时,当然 {withCredentials: true} 它不会出于某种原因将 token 发送到我的后端。在我从 req.headers.cookie 内部的 getServerSideProps 获得的 req 对象中,我看到了 cookie,但它没有发送它。那么当从 getServerSideProps 调用它时手动添加它的解决方案是什么?
编码:
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 存储在客户端(浏览器)上。
可能这可以工作,只要你能让 cookies 到达传奇。
// The axios inside the saga:
const res = yield axios.get(url, {
headers: {
Cookie: "cookie1=value; cookie2=value; cookie3=value;"
});
另一种选择,如果您使用访问 token ,则像使用授权 header 一样发送它。
const res = await axios.get(url, {
headers: { Authorization: `Bearer ${token}` },
});
关于javascript - HttpOnly cookie 不会从下一个 js getServerSideProps 使用 axios 发送(withCredentials : true),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63347884/
我正在使用 NextJS 12。我正在尝试获取本地存储对象。当我在 getServerSideProps 中使用 localstorage 时,我收到类似这样的错误 ReferenceError: l
我在我的 NextJs 应用程序中使用了两个库:next-firebase-auth 和 next-redux-wrapper。它们都需要我用它们各自的函数包装 getServerSideProps。
我正在尝试有条件地 SSR 下一页,但在互联网上缺乏类似情况的答案让我相信我对 NextJS 和 SSR 的理解完全不正确,所以我希望得到一些澄清。 这是我的理解:在我们的页面文件中导出函数 getS
我们只有很少的页面和组件作为服务器端渲染。 我们试图对少数 API 响应使用缓存。 export async function getServerSideProps(context) { con
有没有办法在一个 getServerSideProps() 中从多个 API 路由中获取数据? ? 我有一张表,我需要显示来自多个 MongoDB 集合的数据,并试图弄清楚如何提取这些数据。 本质上,
我正在创建一个页面,我可以在其中概览我的所有笔记/摘要。 笔记的页面是转换为动态文件中使用的 HTML 的 markdown 文件(这可行)。 在笔记页面(列出所有笔记),我想实现一个搜索功能: 我需
我正在调用 getServerSideProps 并传入 req 和 res 参数,如下所示: export async function getServerSideProps({ req, res
您好,按照文档 getServerSideProps是仅在服务器端执行的函数。 export async function getServerSideProps(context) { consol
我正在尝试包装 getServersideProps使用身份验证处理程序函数,但不断收到此错误:TypeError: getServerSideProps is not a function我的包装看
我有一个 Next.js 页面,它在 getServerSideProps() 中从数据库中获取数据(使用 prisma 作为 ORM)。 我的工作基于 this example from an of
有没有一种方法可以让我们拥有类似于在 client-side 上获取数据时的加载状态? ? 我想要一个加载状态的原因是有一个加载骨架之类的东西,例如 react-loading-skeleton在客户
当我在下一个 js 中使用 getServerSideProps 时,页面加载几乎需要 5 或 6 秒。我尝试只使用一个 url 来获取而不是很多,但再次加载页面需要很多时间。 export cons
我想我在这里制造了某种困惑。 根据documentation ,如果我想要页面的服务器端渲染(SSR),我导出异步函数: getServerSideProps 但是,如果我这样做,我将无法构建在本地或
Next.js 9.3 引入 getServerSideProps .在 getInitialProps documentation它现在说: If you're using Next.js 9.3
我正在尝试学习nextjs。正在努力解决 getServerSideProps 的路由问题. 使用免费的 API,我在 DOM 上显示了一个国家列表。我想动态链接到一个国家,并为该特定国家获取和显示数
我从一个新的 Next 应用程序开始,并尽可能使用功能组件而不是基于类的组件。关注documentation ,我没有运气就设置了以下内容: import React from 'react'; im
我从一个新的 Next 应用程序开始,并尽可能使用功能组件而不是基于类的组件。关注documentation ,我没有运气就设置了以下内容: import React from 'react'; im
我正在 pages/post/index.js 中使用 getServerSideProps : import React from "react"; import Layout from "../.
现在,我在 http://localhost:3000/,但在产品中我将在不同的 url,例如 http://example.com/,如何在 getServerSideProps 中获取完整的浏览器
如果有签名用户,我想使用 getServerSideProps 在 Firestore 中获取“示例”文档。下面的代码不起作用。结果是“无法读取”我应该怎么办?或者有其他方法吗? export con
我是一名优秀的程序员,十分优秀!