老是麻烦您看看我这块怎么fetch请求获取不到正确数据?
来源:4-2 网络编程利器-Fetch的基本使用

雪挽
2017-06-14
import React, {Component} from 'react'; import {View, Text, StyleSheet, TouchableOpacity, Image} from 'react-native'; import NavigationBar from './NavigationBar' export default class FetchTest extends Component { constructor(props) { super(props); this.state = { result: '' } } onLoad(url) { fetch(url) .then(response => response.json()) .then(result => { this.setState({result: JSON.stringify(result)}) }) .catch(error=>{ this.setState({result: JSON.stringify(error)}) }) } render() { return ( <View style={styles.container}> <NavigationBar title={'Fetch的使用'}/> <Text onPress={() => this.onLoad('http://rap.taobao.org/mockjsdata/11793/test')}>获取数据</Text> <Text>返回结果:{this.state.result}</Text> </View> ) } } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: 'white' }, text: { fontSize: 20 } })
点击获取数据之后得到的是:
直接走了error不知道为什么~
写回答
2回答
-
可以通过一下方式解决问题:
iOS9之后苹果默认禁用http请求,如要开启可参考:https://segmentfault.com/a/1190000002933776
可以通过调试的方式,打个断点,来帮你快速定位问题,关于如何调试rn可参考:http://www.imooc.com/learn/808
012017-06-15 -
人在塔在_0001
2017-06-17
能说下怎么解决的吗?
012017-06-17
相似问题