简体   繁体   English

AngularJS HTTP POST不发送数据

[英]AngularJS HTTP POST doesn't send data

I have to send a simple JSON object using Angular JS thorough a HTTP POST. 我必须使用Angular JS通过HTTP POST发送一个简单的JSON对象。

I have a simple ng-clik linked function: 我有一个简单的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);
        });

    };

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. POST成功到达我的servlet,返回成功,但是JSON没有放入HTTP消息中,POST数据结果为空。 Why? 为什么?

Try below code. 尝试下面的代码。 actually your posting not a proper key pair,values as json in your post request. 实际上您的发布不是正确的密钥对,在发布请求中的值为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);
        });

    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM