Home  >  Q&A  >  body text

node.js - nodejs处理post表单数据的问题


上图是一个post表单提交到node后台,后台使用的express

如何能实现这样的取值方式:

var name = req.body.person.name;
var age = req.body.person.age;

求大神赐教。。

ringa_leeringa_lee2764 days ago907

reply all(4)I'll reply

  • 阿神

    阿神2017-04-17 14:52:38

    app.use(bodyParser.json());

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 14:52:38

    If you directly use the form element to send http requests to submit form data, use name="person[name]". Actually, a person object will not be automatically generated when sending the request. If you want to use req in node. body.person.XXX to get the submitted content, or use js to submit the form. (If there are not many submitted items, you can list them one by one. If there are many, just traverse this form to get the value.)

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 14:52:38

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

    Add these two sentences and it will be solved.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 14:52:38

    No need to bother, just req.body.xx can get the value!

    reply
    0
  • Cancelreply