簡體   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