javascript - 无法使用 ngx-cookie-service 设置 cookie
问题描述
我正在使用angular 9
,我需要设置一个 cookie。我正在使用ngx-cookie-service 3.0.4
,我正在尝试这样做:
this.cookieService.set("cookieName", user.tokenId, date, "/", "localhost:4200");
当我试图摆脱这个时,我在响应中得到 null,在浏览器中我没有看到这个 cookie。我试图得到如下结果:
console.log("COOKIE: ", this.cookieService.get("cookieName));
我做错了什么?
解决方案
为 localhost 设置 cookie 时,不能'localhost:4200'
用作域。这是设计使然,您可以在此处阅读更多相关信息:具有显式域的 localhost 上的 Cookie
在这种情况下,当应用程序在 localhost 上运行时,您可以完全传入null
或省略域。
const hostName = isLocal ? null : 'HOST_NAME';
this.cookieService.set("cookieName", user.tokenId, date, "/", hostName);
推荐阅读
- java - Jackson为什么要实现Serializable?
- django - Django - 使用 openpyxl 导出 excel 使其更快
- java - 初始化 springboot 创建名为“flywayInitializer”的 bean 时出错
- typescript - 添加
我想强制我的现代页面每 3000 秒刷新一次 >> 所以我开发了一个 spfx 扩展 >> 在我的 .ts 中我添加了以下代码:-
export default class HelloWorldAppli
- r - R 是否在 REML lmer(lmerTest 包)计算中包含对数似然函数的常数?
- python-3.x - 根据 pandas 中的给定条件派生特征或列
- html - 如何在不使用媒体查询的情况下重新排列网格项目
- javascript - 访问模块脚本中定义的 html5 data-* 属性
- angular - 有没有办法手动打开 CKEditor 对话框?
- python - 从本地机器(外部)提交在 Docker 内运行的 Python 脚本时出现问题