0

I have to send a simple JSON object using Angular JS thorough a HTTP POST.

I have a simple ng-clik linked function:

$scope.requestGoogleAuth = function() {
        var googleCredentials = {
            email: '[email protected]',
            password: 'a2s4d'
        };
        console.log(JSON.stringify(googleCredentials));
        /*$http({
            url: '/MyServlet',
            method: "POST",
            data: JSON.stringify(googleCredentials),
            headers: {'Content-Type': 'application/json'}
        })*/
        $http.post("/MyServlet", JSON.stringify(googleCredentials)).then(function success(response) {
            $('#loginGoogleModal').modal('hide');
            $("#notificationsWrapper").notify(
                "Logged with Google",
                {
                    className: 'success',
                    position: 'bottom right'
                }
            );
            $scope.googleLogged = true;
            console.log($scope.googleLogged);
        }, function error(response) {
            $('#loginGoogleModal').modal('hide');
            $("#notificationsWrapper").notify(
                "Failed to login with Google",
                {
                    className: 'error',
                    position: 'bottom right'
                }
            );
            $scope.googleLogged = false;
            console.log($scope.googleLogged);
        });

    };

My controller configuration is:

iftttApp.controller('indexController', 
                    ['$scope', '$routeParams', '$window', '$http', function ($scope, $routeParams, $window, $http, ) { ... });

The POST reaches successfully my servlet returning success, however the JSON isn't put in the HTTP message, the POST data results empty. Why?

3
  • My controller configuration is: iftttApp.controller('indexController', ['$scope', '$routeParams', '$window', '$http', function ($scope, $routeParams, $window, $http, ) { ... });
    – shogitai
    Commented Aug 4, 2016 at 14:44
  • "the JSON isn't put in the HTTP message, the POST data results empty" — How are you determining this? Are you using the developer tools in the browser to examine the network traffic? Are you trying to read it with some server side Java that you didn't include in the question?
    – Quentin
    Commented Aug 4, 2016 at 14:47
  • $scope.nome and $scope.regione are defined ? Commented Aug 4, 2016 at 14:48

1 Answer 1

2

Try below code. actually your posting not a proper key pair,values as json in your post request.

$scope.requestGoogleAuth = function() {
        var googleCredentials = {
            email: '[email protected]',
            password: 'a2s4d'
        };
        console.log(JSON.stringify(googleCredentials));
        /*$http({
            url: '/MyServlet',
            method: "POST",
            data: JSON.stringify(googleCredentials),
            headers: {'Content-Type': 'application/json'}
        })*/

        var postvalue = {
            "nome": $scope.nome,
            "regione": $scope.regione
        }
        $http.post("/MyServlet", angular.toJson(postvalue)).then(function success(response) {
            $('#loginGoogleModal').modal('hide');
            $("#notificationsWrapper").notify(
                "Logged with Google",
                {
                    className: 'success',
                    position: 'bottom right'
                }
            );
            $scope.googleLogged = true;
            console.log($scope.googleLogged);
        }, function error(response) {
            $('#loginGoogleModal').modal('hide');
            $("#notificationsWrapper").notify(
                "Failed to login with Google",
                {
                    className: 'error',
                    position: 'bottom right'
                }
            );
            $scope.googleLogged = false;
            console.log($scope.googleLogged);
        });

    };
4
  • it seems you are posting a string, not a json object Commented Aug 4, 2016 at 14:50
  • thats why i am converting it to JSON using angular.toJson(). it works for me. Commented Aug 4, 2016 at 14:57
  • I correct the code, tested also "angular.toJson(googleCredentials)", but not working.
    – shogitai
    Commented Aug 4, 2016 at 15:01
  • could please check the networking tool in your browser POST body information. and also how you are receiving the json values in server end? Commented Aug 4, 2016 at 15:05

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.