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

标签 javascript angularjs node.js express base64

我正在尝试从我的客户端 (AngularJs) 向我的 NodeJs 服务器发送一组 base64 字符串。 我遇到了一个奇怪的情况。每当我像这样发送对象时:

{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 请求正在运行。此外,当我只向服务器发送一个 base 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 只需提及我将 Node 10 与 express 4 一起使用。

最佳答案

您的服务器告诉您出了什么问题:

The response had HTTP status code 413.

转换为 Request Entity too large

既然你想上传图片,你为什么不简单地使用multipart/form-data来做呢?您真的必须发送 base64 编码的字符串吗?

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

如果您真的需要发送一个字符串,您可以在body-parser 中间件中增加限制:Node.js Express. Large body for bodyParser

关于javascript - Angular JS 将 base64 数组发送到 NodeJs 服务器 - Allow-Access-Control-Origin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31370372/

相关文章:

javascript - 影响相邻元素背景颜色的不透明度过渡

html - 创建服务器/服务客户端

javascript - Node.js:多维数组赋值

javascript - Stripe Connect 费用 - 必须验证为连接帐户才能使用客户参数

javascript - 是否可以使用 pure.js 插入新的 HTML

javascript - 如何在链接单击时将图像加载到 Canvas 中

php - 如何阻止 android 应用程序显示我的网站?

javascript - Angular js RouteProvider 中的问题

javascript - 如何使用 css 样式安全地加载电子邮件内容

node.js - 查询循环中的回调 hell