在问这个问题之前,我确实看过其他类似的问题,但是到目前为止还没有任何帮助。
我有一个使用axios的反应前端,使用快速和快速会话对节点后端进行api调用。一旦我在前端输入登录信息,我就将它发送到我的后端,在那里我设置会话并返回我在localStorage中设置的前端的响应,用于客户端验证。
当我尝试访问受保护的api端点 - 从前端 - 登录后,会话没有我在其中设置的任何信息,因此,给我一个错误。但是,如果我尝试登录并从邮递员访问受保护的端点,它可以正常工作。
这是我的快速会话配置:
router.use(session({
secret: 'notGoingToWork',
resave: false,
saveUninitialized: true
}))
这是api调用(登录后)我通过axios进行:
axios.get(`http://localhost:5000/users/personNotes/${JSON.parse(userData).email}`)
.then(response => console.log(response);
我不知道问题是什么,并希望得到任何帮助。先感谢您!
尝试使用withCredentials
axios(`http://localhost:5000/users/personNotes/${JSON.parse(userData).email}`, {
method: "get",
withCredentials: true
})
要么
axios.defaults.withCredentials = true
你可以使用axios尝试withCredentials
到true
。
使用credentials
获取include
也可以。
fetch(URL,
{
credentials: 'include',
method: 'POST',
body: JSON.stringify(payload),
headers: new Headers({
'Content-Type': 'application/json'
})
})