gpt4 book ai didi

node.js - 无法使用heroku将cookie发送到后端

转载 作者:行者123 更新时间:2023-12-02 02:26:30 24 4
gpt4 key购买 nike

我正在向我的后端发出 axios 请求,并设置一个 cookie:

axios.get(`http://mybackend/somepage`)
.then((response) => {
res.cookie('token', response.data.token)
res.send(`click here <a href="http://mybackend/profile">profile</a>`)
})

在我的后端,我使用带有“cookie-parser”的express。

单击后端的链接,我通过 console.log(req.cookies) 得到此信息:“[Object: null prototype] {}”。

查看请求 header ,cookie 甚至没有发送。

我在本地主机上测试时没有这个问题。

是因为无法在两个域(在本例中为 2 个 heroku 应用程序)之间交换 cookie 吗?

最佳答案

不,您不能在两个 Heroku 应用程序之间发送 cookie...
*.herokuapp.com 已添加到公共(public)后缀列表中,这意味着您无法在这些站点之间交换 cookie。

herokuapp.com is included in the Mozilla Foundation’s Public Suffix List. This list is used in recent versions of several browsers, such as Firefox, Chrome and Opera, to limit how broadly a cookie may be scoped. In other words, in browsers that support the functionality, applications in the herokuapp.com domain are prevented from setting cookies for *.herokuapp.com. Note that *.herokuapp.com cookies can currently be set in Internet Explorer, but this behavior should not be relied upon and may change in the future.
Article

Other StackOverflow问题

修复:

  • 您可以使用自定义域名
  • firstSite.herokuapp.com/secondSite 上提供您的第二个网站

关于node.js - 无法使用heroku将cookie发送到后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65570314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com