简体   繁体   中英

Why is document.cookie an empty string please

I call Set-Cookie on the serverside

export async function post (req, res, next) {
  const jwtToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImNocmlzLmNhcnJpbmd0b25AZ2xldy5pbyIsImlhdCI6MTU5OTA1NDcxNn0.NaEO1ywtCsD55oKb7JSaQbtzidySiy7Bld7Bs63_wuU'
  res.setHeader('Content-Type', 'application/json')
  res.setHeader('Set-Cookie', `jwtToken=${ jwtToken }; Max-Age=86400;`)
  res.end(JSON.stringify({ jwtToken }))
 }

I've tried the browsers Brave and Chrome, why is document.cookie an empty string in both browsers please?

Cookie has /api path in Application > Cookies and is accessed from /sign-in-with-token path.

If a cookie is set with path=/admin, it's visible at pages /admin and /admin/something, but not at /home or /adminpage.

https://javascript.info/cookie#path

Maybe let's try to explicitly set the path to / :

res.setHeader('Set-Cookie', `jwtToken=${ jwtToken }; Max-Age=86400; Path=/`)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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