Node.js/Express form post req.body not working

前端 未结 2 1751
心在旅途
心在旅途 2020-12-01 10:54

I\'m using express and having trouble getting form data from the bodyParser. No matter what I do it always comes up as an empty object. Here is my express generated app.js

相关标签:
2条回答
  • 2020-12-01 11:12
    <form id="myform" action="/" method="post" enctype="application/x-www-form-urlencoded">
      <input type="text" name="I_appear_in_req_body" id="mytext" />
      <input type="submit" id="mysubmit" />
    </form>
    

    The body of a HTTP post is a key/value hash of all the form controls with a name attribute, and the value is the value of the control.

    You need to give names to all your inputs.

    0 讨论(0)
  • 2020-12-01 11:17

    It also due to content type. please see console.log(req) object.

    'content-type': 'application/json; charset=UTF-8’  // valid.
    
    'content-type': 'application/JSON; charset=UTF-8’  // invalid & req.body would empty object {}.
    

    To check content type by console.log(req.is('json')) // return true/false

    I think 'charset=UTF-8' is negligible in above.

    0 讨论(0)
提交回复
热议问题