【问题标题】:Fetch request is not sending my cookies, what is the fault?获取请求没有发送我的 cookie,这是什么问题?
【发布时间】:2020-12-03 21:24:16
【问题描述】:

我的fetch 请求有问题。由于某些原因,当我向我的服务器发出请求时,它没有发送我的 cookie。例如,我正在使用这样的 fetch 请求

fetch('/api/islogged', {
      method: 'GET',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Cache': 'no-cache'
      },
      credentials: 'include',
    })

即使我已经登录,它仍然说我没有。但是,如果我在浏览器中直接转到www.localhost.com/api/islogged,它会返回正确答案

【问题讨论】:

  • 从你所说的看来你正在运行一个本地服务器,所以它不应该是原因,但以防万一,知道 cookie 不能从打开的本地 HTML 文件中工作直接在浏览器中。
  • Fetch API with Cookie的可能重复
  • @VipinKumar 可能不是,因为他使用的是凭据:'include',这是副本中的答案

标签: javascript cookies


【解决方案1】:

尝试使用credentials: 'same-origin' 而不是include。应该可以的。

【讨论】:

    猜你喜欢
    • 2021-07-05
    • 2015-08-29
    • 2011-05-18
    • 1970-01-01
    • 2018-08-03
    • 2012-02-10
    • 2019-05-19
    • 1970-01-01
    相关资源
    最近更新 更多