javascript - How to get POSTed (jquery) array data in Node.js (using express) -


i trying post array server. have difficulties in doing properly.

the array trying post, array of objects dynamically structured, don't know it's length.

to more precise, array of form.

var names =[{id:1, name:"nick"},{id:2,name:"bob"},{id:3,name:"john"}.....{id:n, name:"whatever"}] 

i posting using jquery:

$.post("save_names", {         'names[]': names     }, function(results) {         alert(results);     });  

my node code, following: (i use stormpath-express)

app.post('/save_names', config.access_group, function(req, res) {     console.log("body ", req.body); }); 

this way getting following console.log

body  { 'names[]': [ '[object object]', '[object object]', '[object object]' ] } 

when try print array : console.log("body ", req.body.names); body undefined

can explain why happening? how solve error, , why can't post names:names , work?

you're sending data incorrectly. can examine request in development tools. you'll see this:

form data     names[]:[object object]     names[]:[object object]     names[]:[object object]     names[]:[object object] 

try converting data json yourself:

$.post("save_names", {         'names[]': json.stringify(names)     }, function(results) {         alert(results);     }); 

don't forget correctly access array: console.log("body ", req.body['names[]']);.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -