老是麻烦您看看我这块怎么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
}
})

点击获取数据之后得到的是:

http://szimg.mukewang.com/59414e330001c2be03750687.jpg

直接走了error不知道为什么~

写回答

2回答

CrazyCodeBoy

2017-06-15

可以通过一下方式解决问题:

  1. iOS9之后苹果默认禁用http请求,如要开启可参考:https://segmentfault.com/a/1190000002933776

  2. 可以通过调试的方式,打个断点,来帮你快速定位问题,关于如何调试rn可参考:http://www.imooc.com/learn/808

0
1
雪挽
解决了谢谢老师~
2017-06-15
共1条回复

人在塔在_0001

2017-06-17

能说下怎么解决的吗?


0
1
雪挽
https://segmentfault.com/a/1190000002933776
2017-06-17
共1条回复

React Native技术精讲与高质量上线App开发

一个真实的上线项目,一次完整的开发过程,全面掌握React Native技术

1577 学习 · 727 问题

查看课程