【问题标题】:AngularJS HTTP POST doesn't send dataAngularJS HTTP POST 不发送数据
【发布时间】:2016-08-04 14:42:02
【问题描述】:

我必须使用 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 数据结果为空。为什么?

【问题讨论】:

  • 我的控制器配置是:iftttApp.controller('indexController', ['$scope', '$routeParams', '$window', '$http', function ($scope, $routeParams, $window, $http, ) { ... });
  • “JSON 未放入 HTTP 消息中,POST 数据结果为空” — 您如何确定这一点?您是否使用浏览器中的开发人员工具来检查网络流量?您是否尝试使用问题中未包含的一些服务器端 Java 来阅读它?
  • $scope.nome 和 $scope.regione 已定义?

标签: javascript angularjs json http


【解决方案1】:

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

    };

【讨论】:

  • 看来您发布的是字符串,而不是 json 对象
  • 这就是我使用 angular.toJson() 将其转换为 JSON 的原因。它对我有用。
  • 我更正了代码,也测试了“angular.toJson(googleCredentials)”,但没有工作。
  • 可以在浏览器POST正文信息中查看网络工具。以及您如何在服务器端接收 json 值?
猜你喜欢
  • 2014-06-06
  • 2015-06-02
  • 1970-01-01
  • 2017-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多