Node.js body-parser将不会使用json中的方括号来分析input名称

Node.js中的Express bodyparsing器有问题

我有这样的input:

<input id="personEmail1" type="email" name="person[0][email]" placeholder="Email1" class="form-control"> <input id="personEmail2" type="email" name="person[1][email]" placeholder="Email2" class="form-control"> 

当我提交我的表单后,我在我的console.log中得到了这个:

 { 'person[0][email]': 'info@sss.sss', 'person[1][email]': 'info@ggg.ggg' } 

但我希望它被parsing为json格式:

 { person: [{email: 'info@sss.sss'}, {email: 'info@ggg.ggg'}] } 

我做错了什么?

对于Express 4.x版本,当你需要手动安装body-parser和multer,并且你想从post中得到嵌套的属性,例如req.body.data.testforms的data[test] ,你需要设置:

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

即使您已经使用有效的Javascript语法来处理您的键,但它们仍然是string,并且没有 JSONparsing器会尝试对它们调用eval

然而,JSON已经有了一个数组的概念,它应该以你期待的方式工作。

向我们展示调用console.log的代码会很有用。 但是我猜想,您需要重新考虑您的字段名称的命名约定。

 <input id="personEmail1" type="email" name="personEmail1" placeholder="Email1" class="form-control"> <input id="personEmail2" type="email" name="personEmail2" placeholder="Email2" class="form-control"> 

然后从这些数据创buildJavascript对象。

 function handler(req, res) { var people = [ { email: req.body.personEmail1 }, { email: req.body.personEmail2 } ]; console.log(people[0]); // person 1 console.log(people[1]); // person 2 console.log(people); // both people } 

您正在使用Express身体分析器中间件括号表示法。 但是,作为可以做什么的一个例子…

使用这个视图:

 <form method="post"> <label for="person_email_1">Email address 1</label> <input id="person_email_1" name="person[0][email]" type="email" value="email1@example.com"> <br> <label for="person_email_2">Email address 2</label> <input id="person_email_2" name="person[1][email]" type="email" value="email2@example.com"> <br> <button type="submit">Submit v1</button> </form> <br> <form method="post"> <label for="person_email_1">Email address 1</label> <input id="person_email_1" name="person[email][0]" type="email" value="email1@example.com"> <br> <label for="person_email_2">Email address 2</label> <input id="person_email_2" name="person[email][1]" type="email" value="email2@example.com"> <br> <button type="submit">Submit v2a</button> </form> <br> <form method="post"> <label for="person_email_1">Email address 1</label> <input id="person_email_1" name="person[email]" type="email" value="email1@example.com"> <br> <label for="person_email_2">Email address 2</label> <input id="person_email_2" name="person[email]" type="email" value="email2@example.com"> <br> <button type="submit">Submit v2b</button> </form> <br> <form method="post"> <label for="person_email_1_address">Email address 1</label> <input id="person_email_1_address" name="person[emailAddresses][0][address]" type="email" value="email1@example.com"> <input id="person_email_1_description" name="person[emailAddresses][0][description]" type="text" value="lorem ipsum 1"> <br> <label for="person_email_2_address">Email address 2</label> <input id="person_email_2_address" name="person[emailAddresses][1][address]" type="email" value="email2@example.com"> <input id="person_email_2_description" name="person[emailAddresses][1][description]" type="text" value="lorem ipsum 2"> <br> <button type="submit">Submit v3</button> </form> 

…和这个post处理程序:

 function postHandler(req, res) { console.log(JSON.stringify(req.body)); // show in console res.send(req.body); // show in browser } 

版本1(你的版本,适用于我,并返回你想要的结果)req.body:

 { "person": [ {"email": "email1@example.com"}, {"email": "email2@example.com"} ] } 

版本2a和2b(包含/不包含索引号的string数组)req.body:

 { "person": { "email": [ "email1@example.com", "email2@example.com" ] } } 

版本3(一个对象数组)req.body:

 { "person": { "emailAddresses": [ { "address": "email1@example.com", "description": "lorem ipsum 1" }, { "address": "email2@example.com", "description": "lorem ipsum 2" } ] } } 

我个人使用的版本2和3在节点/ Express / jquery / Bootstrap业务应用程序,其中一个人或企业帐户可以有无限的电话号码,电子邮件地址和URL。 body-parser括号符号使它变得很容易。

我用这个https://github.com/macek/jquery-serialize-object将我的表单序列化为JSON&#x3002; 它根据需要parsing数据,使用起来很简单。

  $.ajax({ type: 'post', data: $form.serializeJSON(), contentType: 'application/json', url: $url, success: function (result) { ... } }); 

我觉得这是最简单的