简体   繁体   中英

ExpressJS receive JSON string but missing some part

I use Ajax with JSON method to send base64 image string to ExpressJS. The whole JSON use console.log to display in client web browser is correct before sending into ExpressJS.

The whole JSON string I cannot show in here due to the length limitation. 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. But sometimes the result like the following:

ng", "other":"abcdefghij"}

OR

{"map":"base64 Strin

UPDATE:

Client upload JSON to server:

$('#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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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