简体   繁体   English

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

[英]ExpressJS receive JSON string but missing some part

I use Ajax with JSON method to send base64 image string to ExpressJS. 我使用带有JSON方法的Ajax将base64图像字符串发送到ExpressJS。 The whole JSON use console.log to display in client web browser is correct before sending into ExpressJS. 发送到ExpressJS之前,整个JSON使用console.log在客户端Web浏览器中显示是正确的。

The whole JSON string I cannot show in here due to the length limitation. 由于长度限制,我无法在此处显示整个JSON字符串。 But the result is similar to the following output: 但是结果类似于以下输出:

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

The ExpressJS most of the time can receive the whole JSON string. ExpressJS大多数时候可以接收整个JSON字符串。 But sometimes the result like the following: 但是有时结果如下:

ng", "other":"abcdefghij"}

OR 要么

{"map":"base64 Strin

UPDATE: 更新:

Client upload JSON to server: 客户端将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');
            }
        });
    });
});

Server side: The error occur in line 4. 服务器端:该错误发生在第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);
        }
    });
};

Error Message: 错误信息:

Unexpected token m in JSON at position 1

OR 要么

Unexpected end of JSON input sometimes

This is because req.on('data') doesn't (always) receive all the data at once. 这是因为req.on('data')不会(总是)一次接收所有数据。

The correct code would be: 正确的代码是:

let raw = ''

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

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

But to use req.on directly is rather low-level, you can probably just use body-parser to achieve what you want. 但是要直接使用req.on是相当底层的,您可能只需使用body-parser即可实现所需的功能。

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

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

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