问题
http://domain.com/action?params[]=1¶ms[]=2¶ms[]=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¶ms=2¶ms=3'); // Name should be "params"
// => { 'params': ['1', '2', '3'] }
qs.parse('params[]=1¶ms[]=2¶ms[]=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