Viny Machado
Viny Machado

Reputation: 629

Angular + Play Framework. When calling $http inside $scope.function server receives null

I'm facing the following problem with Play Framework and AngularJs. I'm trying to send an json object to my play controller but when I called the http request inside a scope function on my angular controller the server receives null. If I put the http call outside the scope function the server receives the json just fine. I appreciate any help guys

=== Scope Function ===

 $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();
            });
        }

=== On My Play Controller ===

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

=== Client Repository ===

'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;
    }]);
});

Upvotes: 1

Views: 251

Answers (1)

Viny Machado
Viny Machado

Reputation: 629

It seam the problem was with the size of my json data. After I reduce the size of the json sent everything started working againg

Upvotes: 0

Related Questions