为什么 post 请求不发送 cookie?

时间:2021-04-11 02:42:53

标签: javascript node.js

我正在尝试将包含我的刷新令牌的 HTTPonly cookie 发送到我的服务器。 cookie如下图

res.cookie('refreshtoken', token, {
    httpOnly: true,
    sameSite: 'strict',
    path: '/refresh_token',
    expires: new Date(Date.now() + 7 * 216000000)   //    Token expires in 7 days
  });

我正在向在同一端口/域上运行的服务器发出发布请求,因此它不需要 cors。当我查看发送的请求时,没有发送 cookie。我在本地主机上运行我的服务器,这可能会导致问题,但我不确定。

无论我使用同源还是包含凭据,都不会发送 cookie。

const result = await (
          await fetch('http://localhost:5000/api/refresh_token', {
            method: 'POST',
            credentials: 'same-origin',
            headers: {
              'Content-Type': 'application/json',
            }
          })).json();

1 个答案:

答案 0 :(得分:0)

对于以后阅读本文的任何人,路径指的是绝对路径。所以 'api/refresh_token' 不会与 '/refresh_token' 匹配。这允许发送我的 cookie。