我在这里遇到了一些问题,我混合使用 Angular2 和 Nativescript 的原生 JS 来使用 HTTP,因为在此实现之前我遇到了 Angular2 HTTP 问题,但我对如何将 JSON 响应从对 monthList 变量的 http 请求,以便可以在所有地方读取它。请帮助,谢谢。
这是我的代码:
public monthList = [];
constructor(location: Location, private page: Page, private router: Router, private testing: any) {
this.router = router;
this.testing = testing;
let http = require("http");
http.getJSON("link")
.then(function (r) {
// Argument (r) is JSON!
}, function (e) {
// Argument (e) is Error!
console.log(e);
});
}
最佳答案
在你的 .then 中,你将你的返回对象设置为你的 monthList
this.monthList = r;
尝试执行 console.dump(r) 并查看返回的内容。您可能必须执行类似 r.xxx 的操作,或者它的结构。
然后你可能会做类似的事情
this.monthList.month
并访问当月的数据等等。或者你可能必须做类似的事情
this.monthList[0].month.
例如,在我的项目中,我返回了一个嵌套的 JSON 对象,我需要将其设置为类似于 r.value.data 的对象。
关于javascript - Nativescript:如何将 HTTP 值存储到公共(public)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38544749/