Javascript ExpressJS接收JSON字符串,但缺少某些部分

Javascript ExpressJS接收JSON字符串,但缺少某些部分,javascript,node.js,ajax,express,Javascript,Node.js,Ajax,Express,我使用Ajax和JSON方法将base64图像字符串发送到ExpressJS。在发送到ExpressJS之前,在客户端web浏览器中显示的整个JSON使用console.log是正确的 由于长度限制,我无法在这里显示整个JSON字符串。但结果与以下输出类似: {"map":"base64 String", "other":"abcdefghij"} ExpressJS大部分时间都可以接收整个JSON字符串。但有时结果如下: ng", "other":"abcdefghij"} 或 更新: 客

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

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

{"map":"base64 String", "other":"abcdefghij"}
ExpressJS大部分时间都可以接收整个JSON字符串。但有时结果如下:

ng", "other":"abcdefghij"}

更新:

客户端将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

试着这样写- {“map”:“base64字符串”,“other”:“abcdefghij”}

试着这样写-
{“map”:“base64字符串”,“other”:“abcdefghij”}

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

正确的代码是:

let raw = ''

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

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

但是直接使用
req.on
是相当低级的,您可能只需要使用body解析器即可实现所需的功能。

这是因为
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来实现您想要的功能。

SinLok,您是否尝试过从express服务器字符串化json并在客户端进行解析。@Vishnudev base64图像字符串从客户端发送到服务器。不是服务器到客户端。我在发送操作之前使用JSON.stringify。但是web浏览器中显示的整个字符串是正确的。您可能需要显示如何在服务器端读取输入JSON。您可以将客户端代码的AJAX部分发布到服务器上吗?@Vishnudev请参阅我的更新部分。SinLok,您是否尝试从express服务器字符串化json并在客户端解析。@Vishnudev base64图像字符串从客户端发送到服务器。不是服务器到客户端。我在发送操作之前使用JSON.stringify。但是web浏览器中显示的整个字符串是正确的。您可能需要显示如何在服务器端读取输入JSON。您可以将客户端代码的AJAX部分发布到服务器上吗?@Vishnudev请查看我的更新部分。抱歉。也许我的解释不清楚。我已经使用了JSON.stringify.Sorry。也许我的解释不清楚。我已经使用了JSON.stringify。
let raw = ''

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

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