如何在 fetch 请求中设置 cookie?

Roo*_*t H 1 javascript fetch-api

我对一个特定问题感到困惑。我正在尝试使用 fetch api 设置 cookie,但我无法这样做。

static deleteorder = (domain = "http://localhost:8080/pc", order) => {

  let cooks = "JSESSIONID=AFAFWEEVV2323GG";
  fetch(deleteorder, {
      method: `GET`,
      credentials: 'same-origin',
      headers: {
        "Content-type": `application/x-www-form-urlencoded`,
      },

    })
    .then(res => {
      console.log(res.status);
    })
    .catch(error => {
      console.log(error);
    });
};
}
Run Code Online (Sandbox Code Playgroud)

当函数运行并200返回但数据保持不变时,意味着顺序保持不变。你们中的一个人能透露一下吗?我正处于学习 fetch api 的初级阶段,因此我们将不胜感激任何反馈。

Bar*_*mar 6

Fetch()只是发送当前文档的 cookie,因此只需将 cookie 添加到document.cookie.

static deleteorder = (domain = "http://localhost:8080/pc", order) => {

  document.cookie = "JSESSIONID=AFAFWEEVV2323GG";
  fetch(deleteorder, {
      method: `GET`,
      credentials: 'same-origin',
    })
    .then(res => {
      console.log(res.status);
    })
    .catch(error => {
      console.log(error);
    });
};
Run Code Online (Sandbox Code Playgroud)