Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native RNCookieManagerAndroid.get获得4个参数,预期为3个参数_React Native_React Native Android - Fatal编程技术网

React native RNCookieManagerAndroid.get获得4个参数,预期为3个参数

React native RNCookieManagerAndroid.get获得4个参数,预期为3个参数,react-native,react-native-android,React Native,React Native Android,以下是代码: componentDidMount () { // Get cookies as a request header string CookieManager.get("http://10.42.0.1:8000/login", (err, res) => { // Outputs 'user_session=abcdefg; path=/;' fetch("http://10.42.0.1:8000/login", {

以下是代码:

componentDidMount () {
    // Get cookies as a request header string
    CookieManager.get("http://10.42.0.1:8000/login", (err, res) => {

      // Outputs 'user_session=abcdefg; path=/;'
      fetch("http://10.42.0.1:8000/login", {
        method: "POST",
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
          'X-CSRFToken': res.csrftoken,
        },
        body: JSON.stringify({
          username: 'user',
          password: 'securepassword',
        })
      })
      .then((response) => response.json())
      .then((responseJson) => {
        console.log(responseJson);
      })
    });
  }
上面的代码显示模拟器上的错误

Android仿真器截图--

此处安装

yarn add react-native-cookies
以及其他给定的指示。 也在app.js文件中导入

import CookieManager from 'react-native-cookies';
一切似乎都是正确的,但仍然显示出这个错误。。 欢迎提出任何建议。。
谢谢你

模块react native Cookie在其中实现承诺,并将响应作为承诺对象提供,因此您唯一做错的是处理Cookiemanager.get调用响应的方式。 按以下方式更改代码,您就可以开始了:

componentDidMount () {
// Get cookies as a request header string
CookieManager.get("http://10.42.0.1:8000/login")
  .then((res) => {

  // Outputs 'user_session=abcdefg; path=/;'
  fetch("http://10.42.0.1:8000/login", {
    method: "POST",
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'X-CSRFToken': res.csrftoken,
    },
    body: JSON.stringify({
      username: 'user',
      password: 'securepassword',
    })
  })
  .then((response) => response.json())
  .then((responseJson) => {
    console.log(responseJson);
  })

  }).catch((err) => {
      //handle your error here 
  })
}

这就是你做错的地方。如果您还需要什么,请告诉我。

是的,非常感谢我在两天的时间里遇到了麻烦,现在没有显示任何错误,而且提取方法工作正常,但是CookieManager。gethttp://10.42.0.1:8000/login 在服务器上没有给出任何响应意味着只有fetch使用用户名和密码访问服务器。我想随CookieManager.get一起提供的CSRFToken不在了,我该怎么办…您想查看django服务器的结果以获取明确信息吗?不知道django服务器。我认为你需要为这个问题添加一个不同的问题。上述问题的问题已解决: