繁体   English   中英

出于某种原因 document.cookie 不工作

[英]For some reason document.cookie not working

我有一个 cookie,它在成功登录到复杂的 PWA 时在服务器中设置。 但是,我无法在注销时从浏览器中删除该 cookie。 为了进一步测试,我有一个(伪-我的意思是它仍然是原始索引。html,但使用 web 组件我可以模拟页面更改)测试页面,它试图删除 cookie,然后向api 服务器(通过 Nginx 代理)如果找到则打印出 cookie。

执行此操作的代码如下

  _cookie(e) {
    const before = document.cookie;
    console.log('before"'+before+'"');
    document.cookie = 'PASv5=;Max-Age=-1;Path=/;Secure;HttpOnly';//delete cookie
    const after = document.cookie;
    console.log('after"'+after+'"');
    this._ping(e);
  }
  _ping(e) {
    e.stopPropagation();
    fetch(`/api/ping/0`, {
      credentials: 'same-origin',
      method: 'post',
      headers: new Headers({
        'content-type': 'application/json'
      }),
      body: '{}'
    }).then(response => {
      if (response.ok) {
        this.reply = 'Successful Ping';
      } else {
        this.reply = `Unsucessful Ping status ${response.status}`;
      }
    }).catch(e => {
      this.reply = 'Network error with error' + e.toString()
    });
  }

单击按钮调用_cookie方法的位置。

此处显示的 console.logs:-

before""     ... test-page.js:130
after""      ... test-page.js:133

在 chrome 开发工具中,我仍然可以看到 cookie(我最初登录时来自服务器的那个),但是在 console.log 调用之前和之后似乎什么都没有显示,我也可以看到带有 cookie 的 .network 请求附加请求(并且服务器还打印出它看到带有 cookie 的 ping,因为它是在我最初登录时设置我的服务器的)

它几乎就好像document.cookie什么都不做。

test-page.js 是定义自定义元素 class 的代码(扩展LitElement - 此 PWA 中的所有自定义元素也是如此)。 此阶段的浏览器显示pas.xxx.test/testPage' which has got there by calling

我绝对找不到任何东西来解释我在 console.logs 中看到的内容(即实际上没有定义 cookies)以及为什么我使用我的代码设置 cookie 没有清除它。

有人可以解释发生了什么以及为什么我的 cookie 没有被删除。

可能是因为你设置http only: true在创建cookie的时候将其在服务端创建时的定义改成false可以通过客户端删除

暂无
暂无

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

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