繁体   English   中英

AngularJS HTTP POST不发送数据

[英]AngularJS HTTP POST doesn't send data

我必须使用Angular JS通过HTTP POST发送一个简单的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数据结果为空。 为什么?

尝试下面的代码。 实际上您的发布不是正确的密钥对,在发布请求中的值为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