Jon*_*ang 17
根据反应原生团队的说法,
对于这种特定情况,您可能希望使用环境提供的fetch API.React Native不在节点运行时内运行.
fetch与...类似http.这是一个如何使用它的简短示例:
// Using fetch to POST
fetch(requestURL, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
message: this.state.input,
})
})
// Using fetch to GET
fetch(requestURL)
.then((response) => response.json())
.then((responseData) => {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(responseData),
loaded: true,
});
})
.done();Run Code Online (Sandbox Code Playgroud)
我觉得你现在卡住了.我的理解是,虽然React Native使用nodejs启动并运行,但运行时实际上并不是nodejs,这就是为什么你不能只是requirehttp.
这个封闭的问题几乎就是说,关于nodejs util和request来自nodejs:
https://github.com/facebook/react-native/issues/375
| 归档时间: |
|
| 查看次数: |
7511 次 |
| 最近记录: |