【问题标题】:angular phonegap - my factory not delivering data角度电话间隙 - 我的工厂不提供数据
【发布时间】:2014-07-25 14:48:56
【问题描述】:

我正在尝试创建一个 Phonegap 应用程序。我使用 Angular 作为前端。 我有一个从外部资源获取数据的工厂。在 .config 中,我添加了 access origin * 属性。

当我使用http://debug.build.phonegap.com 进行调试时,它似乎返回了数据。对 API 的调用返回 584 个字节。

所以从工厂到前端的数据似乎存在问题。

我的工厂

    .factory('ActivityService', ['$http', '$location', 'CookieService', function ($http, $location, CookieService) {
    return {

        getActivitiesService: function (data) {

            $http.post('http://www.example.com/api/v1/Activity.php',
                {
                    MethodName: "getActivitiesService",
                    SessionToken: CookieService.getCookie("ua_session_token")
                })
                .success(data)

        },
        postActivityService: function (activity) {
            $http.post('http://www.example.com/api/v1/Activity.php',
                {
                    MethodName: "postActivityService",
                    SessionToken: CookieService.getCookie("ua_session_token"),
                    ActivityName: activity.activityName,
                    ActivityDescription: activity.activityDescription
                }).success(function () {
                    $location.path("/home");
                });

        }
    }
}])

我的控制器

.controller('HomeCtrl', ['$scope', 'ActivityService', function($scope, ActivityService) {
    //GET activities from server / DB

ActivityService.getActivitiesService(function (data) {
    if (jQuery.isEmptyObject(data))
    {
        $scope.emptyJsonArray = "No activities.";
    }
    else
    {

        $scope.$apply(function () { $scope.activities = data; });
    }

    })
}])

在我的 app.js 中,我这样做:

//Manually bootstrapping Angular, after cordova.
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    angular.element(document).ready(function () {
        angular.bootstrap(document, ['attenttoApp']);
    });
};

更新: 谢谢你们俩。我检查了返回数据的内容,返回的是一个错误。 修复了它,现在我可以工作了......

【问题讨论】:

  • 如何确定 584 字节是正确的数据?你检查过数据的内容吗?它可能是从服务器返回的错误消息。 584 字节并不多。
  • 真的,我会调查的

标签: angularjs cordova


【解决方案1】:

在文档中,他们在工厂中使用 return

https://docs.angularjs.org/api/ng/service/$http

这样试试吧

getActivitiesService: function (data) {

        var toreturn = $http.post('http://www.example.com/api/v1/Activity.php',
            {
                MethodName: "getActivitiesService",
                SessionToken: CookieService.getCookie("ua_session_token")
            })
            .success(function(data, status, headers, config) {
                return data;
            })
            return toreturn;
    },

我认为你应该看到承诺的方向,我写了一个关于这个的答案,看看它 For which status codes promise resolving

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多