【发布时间】:2016-08-30 08:17:45
【问题描述】:
抱歉标题中的玩笑。
我目前正在探索 react native 中的 fetch API,但我遇到了一些我无法解决的问题。
所以,我正在尝试从服务器获取消息,我使用 fetch API 以下列方式调用该消息:
var serverCommunicator = {
test: function() {
fetch(baseUrl , {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
})
.then((response) => response.text())
.then((responseText) => {
return (JSON.stringify(responseText));
})
.catch((error) => {
console.warn(error);
}).done();
},
module.exports = serverCommunicator;
当我只使用console.log(responseText) 进行测试时,我的日志给了我正确的信息。但是,现在当我想尝试将代码中的内容作为消息放入 View 时,它不会按预期返回。调用方式如下:
import Method from '../Services/Methods';
.
.
.
<View style={styles.card}>
<CardView
message={Method.test()}
/>
我可以看到这样调用函数test是如何正确调用的,但是由于某种原因,它没有写入消息。
【问题讨论】:
标签: javascript react-native fetch-api