无法获取从角度到节点服务器发送的表单数据

时间:2018-01-28 08:42:57

标签: angularjs node.js multer

我发送form-data以及image-data
但是在FormData内发送时,我无法在节点服务器上获取数据。

我可以在angular上的console.log控制器上看到数据。

角度代码

$scope.saveInfo = function(){
  var formData = new FormData;
  console.log('Update function');
  for(key in $scope.seller){
    formData.append(key, $scope.seller[key]);
  }
  console.log(key, $scope.seller);
  var file = $('#file')[0].files[0];
  formData.append('image', file);

  $http.put('/api/seller/businessInformation', formData, {
    transformRequest: angular.Identity,
    headers: {
      'Content-type': undefined
    }
  }).then(function(res){

  });
}

Node.js的: 我可以在console.log(req.files);上看到图像数据 但console.log(req.body);打印[对象对象]。

3 个答案:

答案 0 :(得分:6)

The best way to tackle this issue is

(i) JSON.stirngify(req.body) in your code.

(ii) See what is the data getting printed, then based on the output you can use req.body.whateverField

EDIT

You can access FormData as,

console.log(req.files.file.name);

and to access FormData you can use

var a = JSON.parse(req.body.name);
console.log(a);

答案 1 :(得分:0)

req.body是一个对象。如果您的表单数据包含名称和年龄。那么您可以在节点中获取这些值,如下所示

req.body.name
req.body.age

答案 2 :(得分:-4)

问题在于ng-model数据绑定 我绑定了这样的数据:'seller.a.b.c' 相反,我做了seller.a并且它有效。

相关问题