获取响应未设置浏览器 Cookie
Fetch Response is not setting browser cookie
我正在使用获取API在我的浏览器中设置cookie。 这是我的请求对象
fetch('/auth',{
method:'POST',
headers:{
'Accept':'application/json',
'Content-Type':'application/json'
},
body: JSON.stringify({
username:this.state.username,
password: this.state.password,
email: this.state.email
})
})
.then(function(response){
console.log(response)
})
.catch(function(err){
console.log(err)
})
在服务器端
db.one('insert into account(username,password,email) values ($1,$2,$3) returning * ',[req.body.username,hash,req.body.email])
.then((result) => {
console.log('successfully registered: ',result)
const id_token = jwtSign(result)
console.log('id_token: ',id_token)
res.cookie('id_token',JSON.stringify(id_token),{ expires: new Date(Date.now() + (24 * 60 * 60 * 1000 * 30 * 12 * 10)), httpOnly: true })
res.send({'id_token':id_token})
})
.catch((err) => {
console.log('There was an error: ',err.message)
res.send(JSON.stringify(err.message))
})
响应实际上具有SET_COOKIE
标头
Set-Cookie:id_token=%22eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTMsInVzZXJuYW1lIjoia2oiLCJpYXQiOjE0Njg2MDk1Njl9.6w46UCTQwpQ4OIiwj-Ae54LLtYUrUgKjMKHJtepkiZk%22; Path=/; Expires=Sun, 24 May 2026 19:06:09 GMT; HttpOnly
但是,我无法在 chrome 的resources
选项卡中找到 cookie。有人遇到过这个问题吗?我不确定我哪里出错了
根据获取
文档,您必须将credentials
设置为 same-origin
或include
以下是文档中的示例:
fetch('/users', {
credentials: 'same-origin'
})
这很奇怪..但是如果我强制获取导航到另一个页面,cookie就会被存储
fetch('/auth',{
method:'POST',
headers:{
'Accept':'application/json',
'Content-Type':'application/json'
},
body: JSON.stringify({
username:this.state.username,
password: this.state.password,
email: this.state.email
})
})
.then(function(response){
console.log(response)
window.location = '/'
})
.catch(function(err){
console.log(err)
})
相关文章:
- php httponly cookie在浏览器关闭时被删除
- CORS-服务器端cookie没有保存在chrome浏览器上
- 为什么可以't我在谷歌浏览器中返回cookie
- 如何从浏览器中获取cookie,从另一个应用程序中删除
- 我无法在歌剧和互联网浏览器中设置cookie
- 谷歌cookie是否存在于chrome,火狐,即在phantomjs中发出某些请求时发送的浏览器
- jquery cookie,在清除浏览器历史记录后仍然设置
- 使用javascript删除浏览器的所有Cookie
- 在 Javascript 中增加浏览器 cookie?(setCookie, getCookie method)
- C++ 使用批处理脚本清除所有浏览器 Cookie
- 如何使用AngularJS在浏览器cookie中存储身份验证承载令牌
- 如何获取所有浏览器cookie
- 如何从firefox扩展中删除浏览器cookie
- 寻找搜索浏览器cookie的方法并确定href="邮件收件人:"链接
- IE浏览器cookie中的特殊字符
- Javascript:如何模拟浏览器cookie实现
- 从跨浏览器cookie中获取值
- 可以将对象写入浏览器cookie
- 获取响应未设置浏览器 Cookie
- 浏览器 Cookie 过期处理