getMoviesFromApiAsync() { return fetch(‘http://facebook.github.io/react-native/movies.json‘) .then((response) => response.json()) .then((responseJson) => { return responseJson.movies; }) .catch((error) => { console.error(error); }); }
You can also use the/syntax in the ES7 standard in the React native app async
await
:
// 注意这个方法前面有async关键字 async getMoviesFromApi() { try { // 注意这里的await语句,其所在的函数必须有async关键字声明 let response = await fetch(‘http://facebook.github.io/react-native/movies.json‘); let responseJson = await response.json(); return responseJson.movies; } catch(error) { console.error(error); } }
Don't forget fetch
to catch an exception that might be thrown, or you might not see any hints when an error occurs.
By default, iOS blocks all non-HTTPS requests. If the interface you are requesting is an HTTP protocol, you first need to add an exception to the app Transport Securty, which you can refer to in detail.
React Native Get Network data