2016-08-04 57 views
0

我必須通過HTTP POST使用Angular JS發送簡單的JSON對象。AngularJS HTTP POST不發送數據

我有一個簡單的NG-CLIK連接功能:

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

    }; 

我控制器配置爲:

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

的POST成功地達到我的servlet成功返回,但是JSON不要放在HTTP消息,POST數據結果爲空。爲什麼?

+0

我的控制器的配置是: 'iftttApp.controller( 'indexController的',[ '$範圍', '$ routeParams', '$窗口', '$ HTTP', 函數($範圍,$ routeParams,$ window,$ http,){...});' – kitsune

+0

「JSON不放在HTTP消息中,POST數據結果爲空」 - 你如何確定這個?您是否使用瀏覽器中的開發人員工具來檢查網絡流量?你是否試圖用一些你沒有在問題中包含的服務器端Java來閱讀它? – Quentin

+0

$ scope.nome和$ scope.regione是否定義? –

回答

2

請嘗試以下代碼。實際上你的帖子不是一個正確的密鑰對,在你的帖子請求中的值爲json。

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

    }; 
+0

看來你發佈的是字符串,而不是json對象 –

+0

那就是爲什麼我使用angular.toJson()將其轉換爲JSON。這個對我有用。 –

+0

我更正了代碼,測試了「angular.toJson(googleCredentials)」,但無法正常工作。 – kitsune