如何通过查询字符串从ajax传递数组到nodejs

时间:2018-01-29 18:15:36

标签: jquery node.js ajax pug

我试图在查询字符串的帮助下传递数组,但是当我使用nodejs获取它时,它充当字符串。

我的查询字符串就像 -

'/home?page='+value;

  where value is [{name : 'a'},{name :'b'},{name : 'c'}]

但问题是我在节点js中发送并应用

console.log(req.query.page[0].name)输出为[object object] 但我想要输出a

请帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

我认为问题可能在于您将原始对象作为参数发送。尝试执行'/home?page=' + JSON.stringify(value)console.log(JSON.parse(req.query.page[0].name))

您可能还需要对参数进行编码,如下所示:ecodeURI(JSON.stringify(value)) 并使用decodeURI进行解码,如下所示:decodeURI(req.query.page[0].name)