如何在获取请求中设置cookie?

问题描述 投票:0回答:1

我对一个特定的问题感到困惑。我正在尝试使用提取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);
    });
};
}

当函数运行并返回200时,但数据保持不变,表示顺序保持不变。你们中的一个可以阐明吗?我正在学习fetch api的初级阶段,因此,我们将不胜感激任何反馈。

javascript fetch-api
1个回答
1
投票

[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);
    });
};
© www.soinside.com 2019 - 2024. All rights reserved.