简体   繁体   English

为什么 post 请求不发送 cookies?

[英]Why isn't the post request sending cookies?

I am trying to send an HTTPonly cookie containing my refresh token to my server.我正在尝试将包含我的刷新令牌的 HTTPonly cookie 发送到我的服务器。 The cookie is shown below cookie如下所示

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

I am making a post request to a server that is running on the same port/domain so it shouldn't need cors.我正在向在同一端口/域上运行的服务器发出发布请求,因此它不需要 cors。 When I take a look at the sent request, there are no cookies sent.当我查看发送的请求时,没有发送 cookies。 I'm running my server on localhost which may be causing the issue, but I'm not sure.我在 localhost 上运行我的服务器,这可能会导致问题,但我不确定。

Regardless if I use same-origin or include for the credentials, no cookie is sent.无论我使用同源还是包含凭据,都不会发送 cookie。

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

For anyone reading this in the future, path refers to the absolute path.对于将来阅读本文的任何人,路径是指绝对路径。 So 'api/refresh_token' won't match with '/refresh_token'.所以 'api/refresh_token' 不会与 '/refresh_token' 匹配。 This allowed my cookie to be sent.这允许发送我的 cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM