0
當我在React Native應用程序中收到我的JSON時遇到問題。React原生網絡JSON響應
我做一個簡單的讀取請求得到它
getApplicationList()
{
return fetch('http://192.168.X.X/index.php')
.then((response) => response.json())
.then((responseJson) => {
return responseJson;
})
.catch((error) => {
return 'There has been a problem with your fetch operation: ' + error.message;
});
}
,但是當我在渲染功能做出console.error(getApplicationsList());
,我在Android模擬器得到一個非常奇怪的日誌:
console.error: {"_40":"0","_65":"0","_55":"null","_72":"null"}
在我的PHP文件中,我返回了一個JSON編碼的響應,當我在導航器中輸入我的URL(使用'localhost')時,我可以得到它。
當我從模擬器啓動應用程序時,我用我的計算機IP更改localhost地址,當我在瀏覽器中輸入此URL時,我得到一個Cannot GET /index.php
響應。我不知道這是否正常,我在這一切之間感覺有點失落。
非常感謝您的幫助
這是因爲'fetch'返回一個承諾。把console.error放在第二個.then()函數中,並調用渲染'this.getApplicationList()'中的方法' – Raymond
我在第二個.then()中放置了一個console.error,但是沒有任何東西顯示在模擬器上。看來它不會通過... – Koko
嘗試控制檯記錄您的響應和/或'response.json()' – MattyK14