在nodejs服务器上parsingformdata请求时出错无效

我正在从一个简单的file upload到客户端。 以下是我的尝试。

eReaderBook.controller('browseCtrl',['$scope',"$http",function($scope,$http){ $scope.fileName =""; $scope.imageName =""; $scope.uploadFile = function(){ var data1 = new FormData(); data1.append('input_file_name', $('#fileName').prop('files')[0]); data1.append('input_image_name', $('#imageName').prop('files')[0]); if($scope.fileName!="" && $scope.imageName!="") { $http({ url: '/upload', method: "POST", data: data1 }) .then(function(response) { console.log("success") }, function(response) { // optional console.log("failed") }); } }; }]) 

节点.js

 app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(multer()); app.post('/upload',function(req,res) { console.log("aaa"); res.send("hi") }); 

但是,当我发送请求到nodejs服务器它会引发错误错误:parsing无效的JSON(D:\ MeanTest \ node_modules \ body-parser \ lib \ types \ json.js:83:15)

我在这里做错了什么…

你的代码的问题是你传递的data不是一个valid json ,所以处理时bodyParser.json()会抛出一个error

您可以使用bodyParser.raw(options)作为middlewarebodyParser.raw()返回将所有主体parsing为一个Buffer的中间件。

供参考: https : //github.com/expressjs/body-parser