【发布时间】:2020-05-24 16:25:36
【问题描述】:
我收到了来自我的服务器的响应。响应标头包含有效的 Set-Cookie。 Chrome 响应/Cookie 说我有 1 个 cookie,好的。但是... cookie 没有设置到 DevTools/Application/Cookies
/*
My frontend on Vue
I renamed 127.0.0.1 to www.example.com
So running on www.example.com:80 or just www.example.com
*/
let response = await axios({
method: 'post',
url: 'http://127.0.0.1:3000/api/login', // my Node server on 3000 port
data: {
email : login,
password: password,
},
})
/*
My backend part on Node.js+Express
Running on localhost:3000 or 127.0.0.1:3000
CookieParser() is setted
*/
let options = {
maxAge: 345600000,
httpOnly: true,
path: '/',
}
res
.cookie('test', 'test', options)
.cookie('access_token', token, options) //token contains a JWT string
.send('');
/*
Chrome Response Headers
*/
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 0
Content-Type: text/html; charset=utf-8
Date: Sun, 09 Feb 2020 08:52:22 GMT
ETag: W/"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"
Set-Cookie: test=test; Max-Age=345600; Path=/; Expires=Thu, 13 Feb 2020 08:52:22 GMT; HttpOnly
Set-Cookie: access_token=example; Max-Age=345600; Path=/; Expires=Thu, 13 Feb 2020 08:52:22 GMT; HttpOnly
X-Powered-By: Express
/*
Chrome request headers
*/
Accept: application/json, text/plain, '*/*'
Accept-Encoding: gzip, deflate, br
Accept-Language: ru-RU,ru;q=0.9
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 45
Content-Type: application/json;charset=UTF-8
Host: 127.0.0.1:3000
Origin: http://www.example.com
Pragma: no-cache
Referer: http://www.example.com/
Sec-Fetch-Mode: cors
Sec-Fetch-Site: cross-site
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36
此外,我尝试使用 Microsoft Edge,但问题并没有消失。为什么浏览器知道接收 Cookies 却不能设置 cookie?
【问题讨论】:
-
请求是同源还是跨源?你用的是http还是https?您是否在 cookie 上设置了任何其他指令,例如
Expires、Max-Age、Domain等等? -
这里没有足够的细节来帮助我们。请阅读How do I ask a good question,这真的是“我如何提出人们可以快速准确地回答的问题?”。另请参阅How to create a minimal, reproducible example?。
-
如果您打开 Chrome 调试器并转到网络选项卡,并准确地向我们展示原始请求和响应的样子(特别是这样我们可以看到域并查看确切的服务器发回的cookie,那么我们也许可以知道发生了什么。
-
@skirtle 对不起,伙计们!昨天我有点生气,厌倦了解决这个问题。我已经重写了我的帖子并添加了额外的信息
-
@jfriend00 对不起,伙计们!昨天我有点生气,厌倦了解决这个问题。我已经重写了我的帖子并添加了额外的信息
标签: node.js express vue.js axios postman