React native 在新项目中处理本机获取问题
接下来,我遇到了fetch()的问题,我不理解。我还没有找到一篇文章来阐明这一点React native 在新项目中处理本机获取问题,react-native,React Native,接下来,我遇到了fetch()的问题,我不理解。我还没有找到一篇文章来阐明这一点 fetch() 有关守则: url = "https://api.github.com/repos/octocat/Hello-World"; fetch(url).then(function(resp) { return resp.json(); }) .then(function(json){ console.log(json); }); 在localhost调试器ui上运行此命令将返回以下内容,但没
fetch()
有关守则:
url = "https://api.github.com/repos/octocat/Hello-World";
fetch(url).then(function(resp) {
return resp.json();
})
.then(function(json){
console.log(json);
});
在localhost调试器ui上运行此命令将返回以下内容,但没有console.log(json)
输出:
Promise {_32: 0, _8: null, _89: Array[0]}
在本机调试ui之外的chrome dev tools控制台上运行此命令:
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
Object {id: 1296269, name: "Hello-World", full_name: "octocat/Hello-World", owner: Object, private: false…}
我正在使用教程中所示的最新安装的软件包。我的打包机正在运行node_modules/react native/packager/launchPackager.command;退出代码>,我可以看到http://localhost:8081/index.ios.bundle
。为了验证是否发出了https请求,我在本地主机url上运行fetch()
时监视了本地web服务器日志
有什么想法吗?谢谢 看来我和你遇到了同样的问题
您的问题给了我一些提示,让我理解发生了什么,对于以下代码:
fetch(url, option).
then((res) => {
const json = res.json();
console.log(json);
return json;
}).then(json => {
console.log(json);
return json;
});
console.log中的第一个json
实际上是一个Promise
。所以它不会打印正确的内容。但是第二个console.log
将具有正确的json
,因为then
函数将解析返回的承诺 不知道为什么,但是fetch
现在在一个应用程序中为我工作,但仍然不在调试控制台中。