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

I am trying to post an array to my server. But I have difficulties in doing it properly. The array I am

I am trying to post an array to my server. But I have difficulties in doing it properly.

The array I am trying to post, is an array of objects which is dynamically structured, thus I don't know it's length.

To be more precise, my array is of the form.

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

I am posting using jquery:

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

My node code, is the following: (I use stormpath-express)

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

This way i am getting the following from the console.log

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

When i try to print the array : console.log("body ", req.body.names); I get body undefined

Can somebody explain why this is happening? How to solve my error, and why can't I just post names:names and simply work?

I am trying to post an array to my server. But I have difficulties in doing it properly.

The array I am trying to post, is an array of objects which is dynamically structured, thus I don't know it's length.

To be more precise, my array is of the form.

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

I am posting using jquery:

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

My node code, is the following: (I use stormpath-express)

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

This way i am getting the following from the console.log

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

When i try to print the array : console.log("body ", req.body.names); I get body undefined

Can somebody explain why this is happening? How to solve my error, and why can't I just post names:names and simply work?

Share Improve this question edited May 15, 2015 at 12:46 cs04iz1 asked May 15, 2015 at 12:38 cs04iz1cs04iz1 1,8151 gold badge20 silver badges30 bronze badges 3
  • 2 things: set the jQuery post content-type to application/json, and set bodyParser in express middleware that can read json. I suspect you're using regular www-form-urlencoded, and it can't do anything else but simple key-value. – Zlatko Commented May 15, 2015 at 12:51
  • @Zlatko, Could you give me an example? – cs04iz1 Commented May 15, 2015 at 13:00
  • Well, it's simple, client-side: $.ajax({ url:url, type:"POST", data:data, contentType:"application/json", dataType:"json", success: function(){ ... } }) And on express server, for example app.use(bodyParser.json()) or whatever middleware you use. – Zlatko Commented May 15, 2015 at 13:03
Add a ment  | 

2 Answers 2

Reset to default 4

You're sending your data incorrectly. You can examine request in Development tools. You'll see something like this:

Form Data
    names[]:[object Object]
    names[]:[object Object]
    names[]:[object Object]
    names[]:[object Object]

Try converting data to JSON yourself:

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

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

Yes, you req.body contains key names[], not names. So you can either grab from req.body['names[]'] or rewrite code to have name object:

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

And express code:

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

P.S. probably you grab [] names from a GET Query. It's not how POST works.

UPDATE:

I also don't notice, that there is just string of object, so initialize bodyParser.

First install body-parser:

npm install --save body-parser

Then modify code to this:

var express = require('express')
var bodyParser = require('body-parser')

var app = express()

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745167801a4614739.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信