角度+游戏框架。在$ scope.function服务器中调用$ http时,会收到null

时间:2015-03-09 01:57:35

标签: angularjs rest playframework playframework-2.0

我在使用Play Framework和AngularJs时遇到以下问题。我正在尝试将json对象发送到我的播放控制器,但是当我在角度控制器上的作用域函数内调用http请求时,服务器会收到null。如果我把http调用放在scope函数之外,服务器就会收到json就好了。我感谢任何帮助人员

===范围功能===

 $scope.addComment = function () {
            $scope.userComment = {};
            $scope.userComment.client = $scope.client;
            $scope.userComment.description = $scope.comment;

    //Here is made the request to my server. If I put this piece of code out of
    //this scope function works fine
            ClientRepository.addComment($scope.userComment).then(function (response) {
                $scope.comment = '';

                $scope.client.contactsClientHistory.unshift(response);
                $scope.tableComments.total = $scope.client.contactsClientHistory.length;
                $scope.tableComments.reload();
            });
        }

===在我的Play控制器上===

public static Result addComment() {
    try {
        JsonNode request = request().body().asJson(); // Returns null
....More code here

===客户存储库===

'use strict';

define(['app', 'AbstractRepository'], function (app) {

    app.factory('ClientRepository', ['Restangular', 'AbstractRepository', function (restangular, AbstractRepository) {

        function ClientRepository() {
            AbstractRepository.call(this, restangular, 'client');

            this.addComment = function (newResource) {
                return this.restangular.all(this.route + "/comment").post(newResource);
            }
        }

        AbstractRepository.extend(ClientRepository);
        return new ClientRepository();
    }]);

});

=== AbstractRepository ===

'use strict';

define(['app', 'Alert'], function (app) {

    app.factory('AbstractRepository', [ 'Alert', function (Alert) {
        function AbstractRepository(restangular, route) {
            this.restangular = restangular;
            this.route = route;

            restangular.setErrorInterceptor(function (response, deferred, responseHandler) {
                if (response.status == 400 || response.status == 500) {
                    Alert.handle(response);
                } else if(response.status == 403){
                    Alert.error("commons.message.error.authFailure");
                }
                return true;
            });
        }

        AbstractRepository.prototype = {
            getList: function (params) {
                return this.restangular.all(this.route).getList(params);
            },
            get: function (id) {
                return this.restangular.one(this.route, id).get();
            },
            update: function (updatedResource) {
                return updatedResource.put();
            },
            create: function (newResource) {
                return this.restangular.all(this.route).post(newResource);
            },
            remove: function (id) {
                return this.restangular.one(this.route, id).remove();
            },
            getPaginatedResult: function (params) {
                return this.restangular.one(this.route).get(params);
            }
        };

        AbstractRepository.extend = function (repository) {
            repository.prototype = Object.create(AbstractRepository.prototype);
            repository.prototype.constructor = repository;
        };

        return AbstractRepository;
    }]);
});

1 个答案:

答案 0 :(得分:0)

问题在于我的json数据的大小。在减少了json的大小之后,一切都开始起作用了