繁体   English   中英

ExpressJS接收JSON字符串,但缺少一部分

[英]ExpressJS receive JSON string but missing some part

我使用带有JSON方法的Ajax将base64图像字符串发送到ExpressJS。 发送到ExpressJS之前,整个JSON使用console.log在客户端Web浏览器中显示是正确的。

由于长度限制,我无法在此处显示整个JSON字符串。 但是结果类似于以下输出:

{"map":"base64 String", "other":"abcdefghij"}

ExpressJS大多数时候可以接收整个JSON字符串。 但是有时结果如下:

ng", "other":"abcdefghij"}

要么

{"map":"base64 Strin

更新:

客户端将JSON上传到服务器:

$('#btn_floor_upload').click(function () {
    var map_image_obj = new Image();
    map_image_obj.src = URL.createObjectURL($('#select_map_file').prop('files')[0]);
    $(map_image_obj).one('load', function () {
        var canvas = document.createElement("canvas");
        canvas.height = window.canvas_height;
        canvas.width = window.canvas_width;
        var ctx = canvas.getContext("2d");
        ctx.drawImage(map_image_obj, 0, 0, canvas.width, canvas.height);

        // above action for resizing image

        var upload_data = {
            map: canvas.toDataURL("image/jpeg", 0.2),
            height: window.canvas_height,
            width: window.canvas_width,
            floor_name: $('#floor_name').val()
        };


        $.ajax({
            type: "POST",
            url: "/edit/upload_floor",
            data: JSON.stringify(upload_data),
            contentType: "application/json; charset=utf-8",
            dataType: "JSON",
            timeout: 3000,
            success: function (result) {
                if (result.uploaded) {
                    $('#floor_list').append(new Option($('#floor_name').val(), result.floor_id));
                    $('#floor_name').val("");
                    $('#select_map_file').val("");
                    $('#btn_delete_floor').attr("disabled", false);
                    $('#floor_dialog').modal('toggle');
                }
            },
            error: function () {
                $.notify({
                    message: 'Server Error, Please upload the image again!'
                }, {
                    type: 'danger',
                    delay: '5000'
                });
                $('#floor_dialog').modal('toggle');
            }
        });
    });
});

服务器端:该错误发生在第4行中。

upload_floor(req, res){
    req.on('data', function(data) {
        try {
            var json = JSON.parse(data.toString());
            var floor_id = buildingFloor.upload_map(json.floor_name, json.map, json.height, json.width, req.session.username);
            res.send(JSON.stringify({floor_id: floor_id, uploaded:true}));
        }catch(err){
            console.log(err);
        }
    });
};

错误信息:

Unexpected token m in JSON at position 1

要么

Unexpected end of JSON input sometimes

这是因为req.on('data')不会(总是)一次接收所有数据。

正确的代码是:

let raw = ''

req.on('data', function(data) {
  raw += data
})

req.on('end', function() {
  // so something with `raw` here
})

但是要直接使用req.on是相当底层的,您可能只需使用body-parser即可实现所需的功能。

尝试以这种方式编写-{“ map”:“ base64 String”,“ other”:“ abcdefghij”}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM