node.js - 为什么 axios cookie 未定义?
问题描述
当我尝试使用邮递员访问端点时,一切正常,所以我认为问题可能出在我的 axios 请求上,因为req.headers.cookies
在执行此 axios 请求后登录服务器时,值是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.
此错误指向解析函数。
有谁之前经历过这个吗?关于如何解决此问题的任何想法?
解决方案
在 AxiosPOST
中,第一个参数是url
,第二个参数是data
,第三个参数是 for options
。
withCredentials: true
在第三个参数中提供Axios.post
useEffect(() => {
(async () => {
const res = await axios.post('http://localhost:4000/refresh_token', {} ,{
withCredentials: true,
});
})();
}, []);
推荐阅读
- java - Java:转义 XML 文本内容而不是整个文本
- python-3.x - 有没有办法将烧瓶服务器与其他简单的文件传输服务器通信?
- python - Matrix.itam((i,j)) 无法正常工作
- java - 如何为 Java 项目中的类文件配置模板
- laravel - 尝试使用 laravel 向动态接收者发送电子邮件
- android - 在向左、向右、向上、向下滑动时
- timer - RxJS interval() varies wildly in MS Edge
- web-scraping - 如何修复python在.csv文档中返回多行而不是一行?
- ios - 如何使用 SWIFT 4 创建 HMAC MD5 十六进制签名?
- javascript - 如何在日期范围内的 n 天后获取日期?