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]);
}
});
我需要什麼樣的變化做什麼呢?
好吧,我該如何解碼從服務器返回的json響應? – anu
當你在上面的例子中存儲json數據到abc中,然後使用map來存儲它。像這樣: - return this.state.albums.map(albumon => //這裏albumon是包含你的json的所有值的數組對象 –
對不起,我對axios方法感到困惑,我嘗試使用fetch方法工作,當我發送JSON時,現在我正在嘗試解碼返回的響應。是否stringify.json會工作? – anu