Javascript: send object with object inside trought post method -
i'm pretty new javascript , i'm trying send object other object inside trought post method:
$.post('/posttest', {tablename : 'wstest', data : {name : "rachid", age : 42, ville : "tokyo"}}).done(function(data) { console.log("data posted : ", data); });
i can retrieve tablename
req.body.tablename
req.body.data
give me undefined. when console.log(req.body)
got:
{ tablename: 'wstest', 'data[name]': 'rachid', 'data[age]': '42', 'data[ville]': 'tokyo' }
as far understand it, javascript takes data dico ? how can make data object ?
standard form encoded data flat data structure. key/value pairs.
a non-standard extension syntax introduced php allows describe nested data, parser doesn't appear recognise it.
to access data need mention square brackets.
req.body["data[name]"] // etc
alternatively, find parser recognise square brackets having special meaning.
assuming using (fairly common) "url-encoded form body parser" feature of body-parser
module, see the docs:
extended
the extended option allows choose between parsing url-encoded data querystring library (when false) or qs library (when true). "extended" syntax allows rich objects , arrays encoded url-encoded format, allowing json-like experience url-encoded. more information, please see qs library.
so:
app.use(bodyparser.urlencoded({ extended: true }));
… trick.
Comments
Post a Comment