2017-08-01 58 views
0

我正在構建一個包含登錄和註冊模塊的反應本機中的小應用程序。對於通過身份驗證的用戶登錄,我試圖使用JSON獲取數據。我使用PHP作爲後端。我的代碼反應原生如下登錄。來自JSON響應的身份驗證用戶登錄-React Native

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]); 
     } 
    }); 

我需要什麼樣的變化做什麼呢?

回答

0

如果你想獲取來自服務器的JSON數據,然後library.if要安裝這個庫,只需運行命令

npm install --save axios 
在命令提示符下

在項目文件夾,你應該使用Axios公司。在愛可信代碼

實施例反應 - 天然: -

state = { abc:[] }; 

componentWillMount() { 

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

ABC包含用於進一步使用這個數據的您的取出data.Then使用地圖。 關於axios的更多詳細信息,請參閱文檔 https://www.npmjs.com/package/axios

+0

好吧,我該如何解碼從服務器返回的json響應? – anu

+0

當你在上面的例子中存儲json數據到abc中,然後使用map來存儲它。像這樣: - return this.state.albums.map(albumon => //這裏albumon是包含你的json的所有值的數組對象 –

+0

對不起,我對axios方法感到困惑,我嘗試使用fetch方法工作,當我發送JSON時,現在我正在嘗試解碼返回的響應。是否stringify.json會工作? – anu