React Native Fetch Api 无法设置 Cookie Header

React Native Fetch Api Unable to Set Cookie Header

我正在开发一个反应本机应用程序和 php 休息服务作为后端。

当用户登录时,我将用户和 session id 信息回显为 $response 数组,具有 json_encode() 功能。

$response['user'] 存储用户信息。 $response['sessid'] 存储 session id。

在 Postman 中,我可以将 Header 设置为 Cookie,并使用 sessionid 作为值。它工作得很好。但是在 fetch api 中我无法设置 Cookie header.

这是我的代码:

const value = await AsyncStorage.getItem('sessid');
console.log("value" + value)
var url = 'http://192.168.1.27/zuper/test.php';
fetch(url, {
  method: 'GET', 
  headers:{
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'Cookie' : value
  }
}).then(  (response) => {
  console.warn(response)
 })

这是值变量的值:0d580f8b8e1e1ead1ce038cb12289a57

此提取的响应带有一个 php 错误,其中显示

user is not defined

来自行 $_SESSION['user']['name']

你是对的。 Cookie 列在 forbidden header names:

These are forbidden so the user agent remains in full control over them.

如果您想在 HTTP 请求中发送 Cookie,则必须先向使用 Set-Cookie 响应 header 的同源发出 HTTP 请求来设置它。