验证用户从JSON响应登录-React Native

时间:2017-08-01 08:58:19

标签: react-native

我正在使用包含登录和注册模块的react native构建一个小应用程序。对于经过身份验证的用户登录,我尝试使用JSON获取数据。我使用PHP作为后端。我的反应原生代码如下登录。

Login.js

fetch('http://localhost/app/signup.php' ,{
        method: 'POST',
        headers: {
            'Accept' : 'application/json' ,
            //'Content-Type': 'application/json' 
        },
        body : JSON.stringify({
            contact : this.state.contact,
            password: this.state.password
        })
    }).then(() => 
    {       
        response => response.json();
        if(response.json == true)
        {
         const routeStack = this.props.navigator.getCurrentRoutes();
         this.props.navigator.jumpTo(routeStack[1]);
        }
        else
        {
        const routeStack = this.props.navigator.getCurrentRoutes();
         this.props.navigator.jumpTo(routeStack[0]);
        }
    });

我需要做出哪些改变?

1 个答案:

答案 0 :(得分:0)

如果你想从服务器获取json数据,那么你应该使用axios库。如果你想安装这个库,只需运行命令

   npm install --save axios 

在项目文件夹中的命令提示符下。

反应中的axios代码示例 - native: -

 state = { abc:[] };

 componentWillMount() {

 axios.get('url')//lowercase a
 .then(response => this.setState({abc:response.data})).catch((error) => {
 console.error(error);
 });
 }

abc包含您提取的数据。然后使用map进一步使用此数据。 关于axios读取文档的更多细节 https://www.npmjs.com/package/axios