Express Parsing Array from POST

时光毁灭记忆、已成空白 提交于 2019-12-01 17:51:23

Ok, from your question I understand that you want to manipulate the POST Body like a json array then use qs library by making extended true

app.use(bodyParser.urlencoded({ extended: true }));

You can send an string instead of array and keep the extended option in false.

From your front js:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
$.ajax("/send", {
     data: { "strArr": fruits.join() },
     type: "POST",
     async: true,
 ......

From your server js:

router.post('/send', function(req, res) {
    var fruits = req.body.strArr.split(",");
    console.log(fruits); // This is an array
});

change extended option to true it will refrain from turning it to a json

You need to parse the second part of your object so that Node converts it from string to array.

JSON.parse(req.body.arrayOfObjects);

will return you the wanted array for the second part of your object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!