【发布时间】:2020-08-10 17:00:49
【问题描述】:
我想将一个 json 数组发布到节点 js。我使用 ajax post 方法做了同样的事情。在服务器端,我尝试使用 req.body 访问相同的内容。但结果是字符串的形式。所以我无法一个一个地遍历 json 数组元素。我该怎么做?JSON.parse 或 stringyfy 一切都显示为对象。这是在 req.body 中获得的
req.body: '[{"img_id":"img_1","name":"abc","source":"img/Icon_ABc.
png"},{"img_id":"img_0","name":"flower","source":"img/Icon_flower.png"},
{"img_id":"img_5","name":"panda","source":"img/Icon_panda.png"
}]'
我使用的post方法是ajax。
Dataform.append("finalAray",JSON.stringify(finalArray))
$.ajax({
url: '/api/upload/',
type: 'POST',
processData: false,
contentType: false,
dataType : 'application/json; charset=utf-8',
data: Dataform,
success: function(data){
//................
},
error: function(exception){
alert('error:'+exception);
}
});
【问题讨论】:
标签: javascript node.js json body-parser stringify