【问题标题】:Set-Cookie header not accepted by javascript fetchjavascript fetch 不接受 Set-Cookie 标头
【发布时间】:2021-01-16 01:14:14
【问题描述】:

我这几天一直在尝试从我用 Go 编写的网络服务器设置一个 cookie,一切看起来都正常,但它就是行不通。

我的 Set-Cookie 标头:

Set-Cookie: session_token=token; Path=/; Expires=Date; Max-Age=120000; HttpOnly; SameSite=Strict

我已尝试编辑所有值并删除和添加其他字段,但没有任何帮助。如果我将请求设置为获取并直接导航到带有浏览器 cookie 的链接,并且在 Postman 中它就在那里,但是当我尝试使用 fetch 获取 http 请求时它不起作用。 我知道credentials: "same-origin"credentials: "include" 但它们不起作用。 这是我的 js 获取代码:

const log_in = (e) => {

 // data entry validation and stuff

 const url = "/signin";
  fetch(ip + url, {
    credentials: "same-origin",
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      username: document.getElementById("username").value,
      password: document.getElementById("password").value,
    }),
  }).then(
      () => console.log("recieved")
  );
};

【问题讨论】:

    标签: javascript http cookies fetch setcookie


    【解决方案1】:

    这根本不是我想要的,但如果有人和我在同一条船上,这就是我解决问题的方法......使用 axios。 我不使用节点、纱线或其他任何东西,并且使用 cdn 给了我 cors 错误,所以我只是从本地 cdn 文件中复制了代码并且有效。 您可以在这里查看代码:

    axios({
          method: 'post',
          url: ip + url,
          data: {
            firstName: document.getElementById("username").value,
            lastName: document.getElementById("password").value
          },
          withCredentials: true
        })
    

    我不会将此标记为答案,因为我想使用 fetch 来执行此操作并且没有 3rd 方库,但如果这是某人的最后手段,这可行...

    【讨论】:

      猜你喜欢
      • 2013-05-30
      • 2019-05-18
      • 2021-01-08
      • 2018-09-16
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多