Angular JS将base64数组发送到NodeJs服务器 – Allow-Access-Control-Origin

我想从我的客户端(AngularJs)发送一个base64string数组到我的NodeJs服务器。 我遇到了一个奇怪的情况。 每当我发送对象像这样:

{Price: 1000000, LookingFor: "Rent", RoomsNumber: 4, Area: 1000, PropertyType: "Any", Base64:['data:image/jpeg;base64,/9..'] } 

我得到这样的错误:

 XMLHttpRequest cannot load http://localhost:8080/estate. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 413. 

而当我删除base64的HTTP请求正在工作。 另外当我只发送一个string的基地64到服务器,服务器接受它。

在客户端我使用angular$ http:

 self.PostEstate = function (estate) { console.log(serverLocation + 'estate'); console.log('sending estate', estate); $http.post(serverLocation + 'estate', estate) .success(function (data) { console.log('successfully sent estate', data); $rootScope.$broadcast('postEstate', { IsSucceded: true, _id: data }) }).error(function (err) { $rootScope.$broadcast('postEstate', { Msg: err, IsSucceded: false }) }); } 

而在我的服务器,我正在使用我的应用程序的简单路由:

 app.post('/estate', function (req, res) { var estate = new Estate({ Price: req.body.Price, LookingFor: req.body.LookingFor, RoomsNumber: req.body.RoomsNumber, Area: req.body.Area, PropertyType: req.body.PropertyType, Desc: req.body.Desc, photos: [] }); //Thats mongoose obj estate.save(function (err) { if (err) res.send(err); res.json({ _id: estate._id }); }); }); 

我认为这个post要求太大,我可以删除这个限制吗? 如果不是,你可以build议另一个架构,我可以发送base64arrays到我的服务器? 先谢谢你。 PS只是提到,我用节点10与快递4。

你的服务器告诉你什么是错的:

该响应具有HTTP状态码413。

这转化为Request Entity too large

因为看起来你想要上传图片,为什么不简单地使用multipart/form-data来做呢? 你真的必须发送一个base64编码的string?

请参阅本教程中的示例: https : //uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs

如果您确实需要发送一个string,则可以增加body-parser中间件的限制: Node.js Express。 bodyParser的大身体