javascript - Angular http.post 发送空 json

标签 javascript angularjs json node.js

我在背面有 AngularJS 和 NodeJS 上的应用程序,当我将我的应用程序放在 VPS 上时,Angular 正在发送带有 json 的帖子,但对象完全为空,字段未定义。 这是我的代码:

这是我的 Controller :

app.controller('fullAppController', ['$scope','$http','$location','$cookies',function($scope,$http,$location,$cookies)

我的配置为帖子提供默认选项:

app.config(['$httpProvider', function ($httpProvider) {
  $httpProvider.defaults.headers.post = {};
}]);

我的问题:

    $scope.registerSubmitData = function(){
      if(propertyCheck($scope.registerData,'userName') &&
         propertyCheck($scope.registerData,'email') &&
         propertyCheck($scope.registerData,'password') &&
         propertyCheck($scope.registerData,'passwordCheck') &&
         propertyCheck($scope.registerData,'firstName') &&
         propertyCheck($scope.registerData,'lastName') &&
         propertyCheck($scope.registerData,'street') &&
         propertyCheck($scope.registerData,'streetNum') &&
         propertyCheck($scope.registerData,'postCode') &&
         propertyCheck($scope.registerData,'city') &&
         propertyCheck($scope.registerData,'state')){
          console.log($scope.registerData); //HERE IS FILLED OBJECT
           $http.post('http://localhost:8000/register', $scope.registerData).success(function(data,status){ 
            // HERE ANGULAR POST A EMPTY JSON
            $location.path('/login');
            $scope.logInSuccesAlert = true;
            $scope.logInSuccesAlertText = "Successfully register";
           }).catch(function(err){
             $scope.registerAlert = true;
             $scope.registerAlertText = "Username exist";
           });
         }else{
           $scope.wrongNoState = true;
           $scope.registerAlert = true;
           $scope.registerAlertText = "Please fill all fields";
           $scope.wrongNoStateText = "Please select state";
         }
    }

(我正在使用 cors)我的 NodeJS 接收:

app.use('/register', function(req,res){
   console.log(req.body);
  //HERE RECEIVED JSON (req.body) IS EMPTY
  var registerData = req.body;
  registerData.password = SHA256(registerData.password);
  insertUser(registerData).then(function(result){
    res.send("Successfully register");
  }).catch(function(err){
    res.status(409).end("User exist");
  });
});

如何处理这个问题?

最佳答案

尝试替换:

$http.post('http://localhost:8000/register', $scope.registerData).success(function(data,status){ 

与:

$http.post('http://localhost:8000/register', $scope.registerData, { headers: { 'Content-Type': 'application/json' } }).success(function(data,status){ 

您应该指定内容类型。如果您希望所有 $http.post 都是 json,您可以将其添加到默认选项中。

关于javascript - Angular http.post 发送空 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40260786/

相关文章:

javascript - 从 QML ListView 委托(delegate)调用 JavaScript 对象方法

javascript - 单击时如何更改文本的颜色

angularjs - 两个不同的 Controller 使用相同的服务并获得相同的结果

javascript - 如何访问 Angular 选择元素的选项?

json - Json 格式的 API 日志记录

ios - 在 Objective-C 中解析 JSON

json - 在 Grails 2.3.1 中从 Controller 渲染 JSON 响应

javascript - jQuery 匿名函数 - 嵌套函数

php - 序列化/反序列化javascript并被PHP读取

javascript - 'destroyed' Controller 中的 Angular $rootScope $on 监听器继续运行