我正试图从我的客户端(AngularJs)发送一个base64字符串数组到我的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请求正在工作。同样,当我只发送一个以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
});
});
});
我认为帖子请求太大,我可以取消限制吗?如果不是,你能建议另一种架构,我可以发送base64数组到我的服务器?提前谢谢你。PS只是提到我使用节点10与express 4。
您的服务器告诉您哪里出错了:
响应的HTTP状态码为413。
翻译成Request Entity too large
当它看起来像你想上传图像,为什么不简单地使用multipart/form-data
这样做?你真的需要发送一个base64编码的字符串吗?
参见本教程的示例:https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs
如果你真的需要发送一个字符串,你可以增加body-parser
中间件的限制:Node.js Express。