Nodejs: url.parse issue with arrays inside query

孤者浪人 提交于 2019-12-20 03:00:50

问题


http://domain.com/action?params[]=1&params[]=2&params[]=3

returns:

query: { 'params[]': [ '1', '2', '3' ] }

params[] as name instead of params?

After PHP it's kinda surprise.

jQuery serialization is adding [] on parameters btw.

Are you guys wrote a helper for this or I'm just doing it wrong?


回答1:


This seems like expected behavior to me; I would be more surprised if the querystring parser removed part of the name. That is, the module is doing exactly what I would expect from a parser which simply splits name/value pairs by '&' and name/value by '=' (and unescapes special characters).

var qs = require('querystring');

qs.parse('params=1&params=2&params=3'); // Name should be "params"
// => { 'params': ['1', '2', '3'] }

qs.parse('params[]=1&params[]=2&params[]=3'); // Name should be "params[]"
// => { 'params[]': ['1', '2', '3'] }



回答2:


This module does parsing as required:

https://github.com/visionmedia/node-querystring

There is another one for complex arrays if this doesn't work:

https://github.com/jazzychad/querystring.node.js

Both found here: https://github.com/joyent/node/wiki/modules



来源:https://stackoverflow.com/questions/8448305/nodejs-url-parse-issue-with-arrays-inside-query

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