简体   繁体   English

res.setHeader(“ Set-Cookie”,…)未在Node / Express中设置cookie

[英]res.setHeader(“Set-Cookie”, …) is not setting the cookie in Node / Express

I have tried 我努力了

  res.setHeader(
    "Set-Cookie",
    cookie.serialize("name", "name2", { path: "/", domain: "localhost" })

and

  res.setHeader(
    "Set-Cookie",
    cookie.serialize("name", "name2"))

and I have tried to set the cookie outright with 并且我试图完全设置cookie

res.setHeader(
    "Set-Cookie",
    "name=name"})

but no cookie is set when I examine the res, and no cookie is received in my frontend. 但是在检查资源时未设置cookie,并且前端未接收到cookie。 Everything is run locally. 一切都在本地运行。 I do not understand why. 我不理解为什么。

如果您只想制作一个cookie,请尝试使用它而不是res.setHeader()

res.cookie('cookieName', 'cookieValue');

I found the answer. 我找到了答案。 When using fetch() from the front end I didn't set the credentials flag to include. 从前端使用fetch()时,我未将凭据标志设置为包括。 When I included the flag it worked instantly. 当我包含该标志时,它立即起作用。

  return await fetch(
    `url`,
    { credentials: "include" }

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

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