Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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?

share|improve this question
    
My controller configuration is: iftttApp.controller('indexController', ['$scope', '$routeParams', '$window', '$http', function ($scope, $routeParams, $window, $http, ) { ... }); – kitsune Aug 4 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 Aug 4 at 14:47
    
$scope.nome and $scope.regione are defined ? – oliv37 Aug 4 at 14:48
up vote 2 down vote accepted

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

    };
share|improve this answer
    
it seems you are posting a string, not a json object – oliv37 Aug 4 at 14:50
    
thats why i am converting it to JSON using angular.toJson(). it works for me. – Ravi Durairaj Aug 4 at 14:57
    
I correct the code, tested also "angular.toJson(googleCredentials)", but not working. – kitsune Aug 4 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? – Ravi Durairaj Aug 4 at 15:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.