首页 > 解决方案 > 在 React Native 中没有使用 fetch() 获取 Set-Cookie 标头

问题描述

我是使用带有 React Native 的 Fetch API 的新手,我正在将它用于我的一个项目。我需要fetch()方法来获取set-cookie 标头并获取PHPSESSID值。这是我拥有的代码。

fetch("https://fs.auroville.org.in/", {
     credentials: 'include',
     headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
     }
})
     .then(request => {
          console.log(request.headers)
          //console.log(get_set_cookies(request.headers))
})
     .catch((error) => {
          console.log("Error")
          console.error(error)
});

它似乎可以与我的代码一起使用,但由于某种原因,set-cookie 标头似乎没有出现。我知道它仅在您第一次打开它时才会显示在 Google Chrome 检查选项卡的网络选项卡中,但之后不会。

网络选项卡 首次访问网站时

我想知道是否有人可以提供帮助,因为我做了很多研究,但我似乎找不到可靠的解决方案,即使我第一次调用该函数时,“Set-Cookie”标头也没有显示但每个其他标题都会这样做。

这是 get_set_cookies() 函数的代码

const get_set_cookies = function(headers) {
        const set_cookies = []
        for (const [name, value] of headers) {
                set_cookies.push(value)
        }
        return set_cookies
}

标签: javascriptreact-nativefetchexporesponse-headers

解决方案


推荐阅读