javascript - Nativescript:如何将 HTTP 值存储到公共(public)变量

标签 javascript android angular nativescript angular2-nativescript

我在这里遇到了一些问题,我混合使用 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/

相关文章:

angular - SafeValue 必须使用 [property]=binding 虽然我已经在使用属性绑定(bind)

javascript - 如果 php echo 内容为空则隐藏 div

javascript - Echarts.js : Detect a click on a subset of data

android - 永远不要从 getAuthToken 请求中获取 AccountManager.KEY_INTENT

angular - 如何将 Angular 中的 preserveWhitespaces 选项全局设置为 false?

angular - 如何使用 Azure AD 作为身份提供者为 Angular 7.x 应用程序实现基于 SAML 的身份验证?

可变范围的 JavaScript 循环问题

javascript - 将 PHP 数组解析为 Javascript 的最佳方法是什么?

android - 您使用 NativeScript 的体验如何?

android - 挂载 Android 模拟器镜像