【问题标题】:Why is axios cookie undefined?为什么 axios cookie 未定义?
【发布时间】:2020-10-28 19:47:30
【问题描述】:

当我尝试使用邮递员访问端点时,一切正常,所以我认为问题可能出在我的 axios 请求上,因为在执行此 axios 请求后在服务器上记录 req.headers.cookies 时,值是 undefined

浏览器中的 Cookie 也可以正常工作。

当我在邮递员中执行此请求时,req.headers.cookie 的值很好,并且请求已执行且没有任何错误。

客户端代码:

  useEffect(() => {
    (async () => {
      const res = await axios.post('http://localhost:4000/refresh_token', {
        withCredentials: true,
      });
    })();
  }, []);

服务器代码(端点函数):

export const validateRefreshToken = async (req, res) => {
  console.log(req.headers.cookie); // undefined
  const { token } = parse(req.headers.cookie);

  ...
};

错误信息:TypeError argument str must be a string. 这个错误指向解析函数。

以前有人经历过吗?关于如何解决此问题的任何想法?

【问题讨论】:

    标签: node.js reactjs express cookies axios


    【解决方案1】:

    对于 Axios POST,第一个参数是 url,第二个参数是 data,第三个参数是 options

    Axios.post 的第三个参数中提供withCredentials: true

      useEffect(() => {
        (async () => {
          const res = await axios.post('http://localhost:4000/refresh_token', {} ,{
            withCredentials: true,
          });
        })();
      }, []);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-03
      • 2021-11-14
      • 2017-09-27
      • 2020-05-12
      • 2021-07-22
      • 2021-07-18
      • 2012-06-21
      • 2022-01-05
      相关资源
      最近更新 更多