React Native Fetch Api无法设置Cookie标头

时间:2018-08-15 08:10:22

标签: javascript php react-native fetch

我正在开发一个react native应用和php rest服务作为后端。

当用户登录时,我使用$response函数将用户和会话ID信息作为json_encode()数组回显。

$response['user']存储用户信息。 $response['sessid']存储会话ID。

在邮递员中,我可以将Header设置为Cookie,并将值设置为sessionid。它运作完美。但是在获取api中,我无法设置Cookie标头。

这是我的代码:

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错误,内容为

  

未定义用户

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

1 个答案:

答案 0 :(得分:0)

您是正确的。 Cookie在forbidden header names中列出:

  

这些都是禁止的,因此用户代理仍然可以完全控制它们。

如果您想在HTTP请求中发送Cookie,则必须向使用Set-Cookie响应标头进行设置的原始HTTP发起相同的HTTP请求。

相关问题