javascript - Node.js: Receiving empty body when submitting form -
i'm aware similar questions have been asked lot here, still couldn't solve problem. don't undestand whats going on. i'm trying send data form via post
request keep receiving empty body. jade code form looks this: (edit: added enctype
)
form(enctype="application/json" action="/test" method="post" name="login") table tr th label(for="username") username td input(type="text" name="username" placeholder="username") tr th label(for="password") password td input(type="password" name="password" placeholder="password") tr tr th td input(type="submit" value="login")
which generates following html code (edit: add html code):
<form enctype="application/json" action="/test" method="post" name="login"> <table> <tr> <th> <label for="username">username</label> </th> <td> <input type="text" name="username" placeholder="username"> </td> </tr> <tr> <th> <label for="password">password</label> </th> <td> <input type="password" name="password" placeholder="password"> </td> </tr> <tr></tr> <tr> <th></th> <td> <input type="submit" value="login"> </td> </tr> </table> </form>
so didn't forget name
attribute.
this how handle request:
router.post('/test', function(req, res) { console.log('body:', req.body); res.render('login', {title: 'hk test'}); });
which prints body {}
. sure request received.
i using bodyparser:
var bodyparser = require('body-parser'); app.use(bodyparser.urlencoded({ extended: false })); app.use(bodyparser.json());`
however, when use curl: curl -h "content-type: application/json" -d '{"username":"xyz","password":"xyz"}' http://localhost:4000/login
, body contains username , objects. copied jade code working project, don't understand i'm doing wrong.
does have idea reason this?
to serialize form data json when submitting, have add attribute form:
enctype="application/json"
otherwise, without js, form data not encapsulated json post request.
further reading:
Comments
Post a Comment