【发布时间】:2018-10-18 05:53:30
【问题描述】:
我正在尝试使用 fetch 在 React Native 中获取 HTML 页面的内容,我正在 expo 上运行它,这里:
https://snack.expo.io/@abalja/hellofetch
基本上,代码没有什么特别之处,它使用“fetch”来加载 .json 文件,但我无法让它用于 .html 文件。它只是默默地失败,我什至没有记录错误。我不确定这是 Expo 还是 ReactNative 问题。
const url2 = 'http://www.spiegel.de/sport/fussball/nations-league-italien-trifft-in-der-nachspielzeit-polen-steigt-ab-a-1233219.html#ref=rss'
export default class App extends React.Component {
componentDidMount(){
console.log('did mount, fetching: ' + url2)
fetch(url2)
.then((response) => {
console.log(response) // 1
return response.text()
})
.then((responseText) => {
console.log('fetch text', responseText) // 2
// return responseText.movies;
})
.catch((error) => {
console.error(error);
});
}
render() {
return (
<View style={styles.container}>
</View>
);
}
}
在 1 时,我记录了响应:
{type:"default",status:200,ok:true,headers:{…},url:"http://www.spiegel.de/sport/fussball/nations-league-italien-trifft-in-der-nachspielzeit-polen-steigt-ab-a-1233219.html",_bodyInit:{…},_bodyBlob:{…}}
type:"default"
status:200
ok:true
►headers:{map:{…}}
url:"http://www.spiegel.de/sport/fussball/nations-league-italien-trifft-in-der-nachspielzeit-polen-steigt-ab-a-1233219.html"
►_bodyInit:{_data:{…}}
►_bodyBlob:{_data:{…}}
在 2 时,我完全没有记录任何内容。
【问题讨论】:
-
我无法重现错误,我得到了记录的响应和文本。您能否提供有关您想从该网站检索的内容的更多信息?
-
您是在 expo 上还是在设备上尝试过?
标签: react-native fetch expo