one*_*ach 4 promise nativescript fetch-api
我正在使用 nativescript 为 android 开发应用程序。我有类似的东西
var fetchModule = require("fetch");
fetchModule.fetch("http://202.120.227.11/")
.then(function(resp){
console.log(JSON.stringify(resp));
return resp;
})
.catch(function(err){
console.log(JSON.stringify(err));
return err;
});
Run Code Online (Sandbox Code Playgroud)
但then块永远不会被执行。有时catch块被执行并给出网络错误。但无论哪种情况,根据我的 tcpdump 记录,请求被发送并顺利收到响应。
因此,似乎 nativescript 出于某种原因过滤了响应。
有人经历过吗?
请注意,您resp是一个Response对象,如果您想读取其内容,则需要使用其功能之一:arrayBuffer、blob、formData、json或text。
这些函数读取完成的响应并返回使用读取值解析的承诺。
例如,
fetch("http://202.120.227.11/")
.then(function(resp){
return resp.json();
})
.then(function(val) {
console.log(JSON.stringify(val));
return val;
});
Run Code Online (Sandbox Code Playgroud)