如何使用ajax post方法发送数据?(node.js mogodb)

时间:2015-05-20 06:16:27

标签: javascript ajax node.js mongodb

我想像这样插入我的数据:

{name:john,address:[{houseName:gbu,place:vypin,pin:3322}]} 

但是当我使用req.body变得像这样的ajax发布这个

{name:john,address[0][houseName]:gbu,address[0][place]:vypin,address[0][pin]:3322} 

建议我有任何简单的方法

1 个答案:

答案 0 :(得分:0)

你需要在一个新的变量中重写json:

var json = {
   name: req.body.name,
   address: []
};

req.body.address.forEach(function (address) {
   json.address.push({
      houseName: address.houseName,
      place: address.place,
      pin: address.pin
   });
});