AngularJS HTTP POST不发送数据

时间:2016-08-04 14:42:02

标签: javascript angularjs json http

我必须通过HTTP POST使用Angular JS发送一个简单的JSON对象。

我有一个简单的ng-clik链接功能:

$scope.requestGoogleAuth = function() {
        var googleCredentials = {
            email: 'user@domain.com',
            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);
        });

    };

我的控制器配置是:

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

POST成功地使我的servlet返回成功,但是JSON没有放入HTTP消息,POST数据结果为空。为什么呢?

1 个答案:

答案 0 :(得分:2)

尝试以下代码。实际上你的帖子不是一个正确的密钥对,在你的帖子请求中值为json。

$scope.requestGoogleAuth = function() {
        var googleCredentials = {
            email: 'user@domain.com',
            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);
        });

    };