React Native Fetch Api无法设置Cookie标头 [英] React Native Fetch Api Unable to Set Cookie Header

查看:79
本文介绍了React Native Fetch Api无法设置Cookie标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing a react native app and php rest service as backend.

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

When user login i echo user and session id info as $response array with json_encode() function.

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

$response['user'] stores user info. $response['sessid'] stores session id.

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

In Postman i can set a Header as Cookie and as value i use sessionid. It works perfectly. But in fetch api i can't set 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错误,内容为

Response for this fetch comes with a php error which says

未定义用户

来自行 $ _ SESSION ['user'] ['name']

推荐答案

您是正确的.Cookie列在禁止的标题名称中:

You are correct. Cookie is listed amoung the forbidden header names:

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

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

如果要在HTTP请求中发送Cookie,则必须向使用 Set-Cookie 响应标头进行设置的同一源发出一个先前的HTTP请求.

If you want to send a Cookie in an HTTP request, then you must make a prior HTTP request to the same origin that uses a Set-Cookie response header to set it.

这篇关于React Native Fetch Api无法设置Cookie标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆