首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套的Post数据解析不正确

嵌套的Post数据解析不正确
EN

Stack Overflow用户
提问于 2016-09-23 06:41:55
回答 1查看 304关注 0票数 2

我想用嵌套的post元素发布表单

中的视图

代码语言:javascript
复制
<form method="post" action="">
<input type="text" placeholder="Enter Tag here" class="gui-input" name="reply_message">
<input type="text" placeholder="Enter Label for Decision" class="gui-input" name="decision[0][label]" value="Interested">
<input type="text" placeholder="Enter decision keywords here" class="gui-input" data-role="tagsinput" name="decision[0][keyword]" value="Interested, call me">
<input type="text" placeholder="Enter Label for Decision" class="gui-input" name="decision[1][label]" value="Not Interested">
<input type="text" placeholder="Enter decision keywords here" class="gui-input" data-role="tagsinput" name="decision[1][keyword]" value="not interested, not">
<input type="text" placeholder="Enter Label for Decision" class="gui-input" name="decision[2][label]" value="Call Later"  >
<input type="text" placeholder="Enter decision keywords here" class="gui-input" data-role="tagsinput" name="decision[2][keyword]" value="Interested, call me, later">
<button class="button btn-primary" type="submit">Submit </button>

路由器文件

代码语言:javascript
复制
router.post('/mapping', isLoggedIn, function (req, res, next) {
   var posted_data= req.body;
   console.log(req.body);
   res.send(posted_data);
});

我得到了像这样的数据结构

代码语言:javascript
复制
{
    "reply_message": "This is test",
    "decision[0][label]": "Interested",
    "decision[0][keyword]": "Interested, call me",
    "decision[1][label]": "Not Interested",
    "decision[1][keyword]": "not interested, not",
    "decision[2][label]": "Call Later",
    "decision[2][keyword]": "Interested, call me, later"
}

但是实际发布的数据结构应该是

代码语言:javascript
复制
{
    "reply_message": "This is test",
    "decision": [{
        "label": "Interested",
        "keyword": "Interested, call me"
    }, {
        "label": "Not Interested",
        "keyword": "not interested, not"
    }, {
        "label": "Call Later",
        "keyword": "Interested, call me, later"
    }]
}

那么,我如何才能做到这一点,是否有任何节点模块,我必须使用这样的发布表单数据?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-23 07:30:42

嗯,name="decision[0][label]"工作正常。表单数据作为键值对提交,输入名称成为键。

如果表单是使用HTTP提交的,那么您将在req.query中获得所需的对象。然而,对于HTTP,它是以req.body的形式出现的。

在这里, module可以在服务器端帮助您:

代码语言:javascript
复制
const qs = require('qs');

const input = {
    "reply_message": "This is test",
    "decision[0][label]": "Interested",
    "decision[0][keyword]": "Interested, call me",
    "decision[1][label]": "Not Interested",
    "decision[1][keyword]": "not interested, not",
    "decision[2][label]": "Call Later",
    "decision[2][keyword]": "Interested, call me, later"
};

const output = qs.parse(qs.stringify(input)); 

console.log(output);

// console:
{ reply_message: 'This is test',                                                                                                
  decision:                                                                                                                     
   [ { label: 'Interested', keyword: 'Interested, call me' },                                                                   
     { label: 'Not Interested', keyword: 'not interested, not' },                                                               
     { label: 'Call Later', keyword: 'Interested, call me, later' } ] } 
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39654207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档