javascript - 为什么 $http 请求在此 Cordova 应用程序中不起作用?

标签 javascript angularjs cordova ionic-framework

我试图将代码中的数据带到我的应用程序中,这里是工厂。

angular.module('starter.services', [])

    .factory('Locations', function ($http, $q) {
        // Might use a resource here that returns a JSON array
            var url = "https://portal.mobilequbes.com/api/kiosks?callback=JSON_CALLBACK";
            var locations = function () {
                $http.jsonp(url)
                    .then(function (result) {
                        return result.data;
                    });
            };

        return {
            all: function () {
                return locations;
            },
            get: function (locId) {
                for (i = 0; i < locations.length;i++){
                    if (locations[i].friendlyName == parseInt(locId)) {
                        return locations[i];
                    }
                }
            }
        }
    });

和我的 Controller :

.controller('MapCtrl', function ($scope, $http, Locations) {
    $scope.locations = Locations.all();
    $scope.createMarks = function () {
        createList(Locations.all());
    }
})

加载时,它只加载任何内容或两个如下所示的对象:“”

我不知道为什么,因为我似乎看不出任何问题,我觉得我已经读完了这篇文章。我已经使用 jsFiddle 测试了返回函数,它工作得很好,所以它与 ionic/cordova 有关,我相当确定。

最佳答案

在你的工厂做,

angular.module('starter.services', [])

.factory('Locations', function ($http, $q) {
    // Might use a resource here that returns a JSON array
        var url = "https://portal.mobilequbes.com/api/kiosks?callback=JSON_CALLBACK";
        var locations = [];  //storing locations for future usage

    return {
        all: function () {
            return $http.jsonp(url)
                .then(function (result) {
                    locations = result.data;
                    return result.data;
                });
        },
        get: function (locId) {
            for (i = 0; i < locations.length;i++){
                if (locations[i].friendlyName == parseInt(locId)) {
                    return locations[i];
                }
            }
        }
    }
});

在你的 Controller 中,

.controller('MapCtrl', function ($scope, $http, Locations) {
    Locations.all().then(function (locations) {
         $scope.locations = locations;
    });
    $scope.createMarks = function () {
        createList(Locations.all());
    }
})

现在,Locations.all() 方法返回一个 promise ,该 promise 将解析为 Controller 中的 result.data,并且您可以访问位置。

以前您没有返回任何内容,因此 $scope.locations 未定义。

关于javascript - 为什么 $http 请求在此 Cordova 应用程序中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37949072/

相关文章:

javascript - 可编辑表单需要默认启用编辑模式

java - Phonegap 应用程序在运行 Android ICS 4.0 时抛出错误,但在 Android 2.3 及以下版本上运行良好

ios - InAppPurchase 插件无法从 Objective-C 执行 JS 回调

javascript - d3.js 数据第一次未绑定(bind)

javascript - 在 JavaScript 中获取 DIV 的 z-index?

javascript - 如何做到当我双击 div 时,它不会选择该单词?

jquery - 是否可以在 Angular 中使用 $compile 来创建可导出的 elem?

javascript - 设置 iframe.src (使用 jquery)时如何将 cookie 附加到请求?

javascript - 为什么我收到错误参数未定义?

javascript - 优化 Phonegap 应用程序的图像