最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - POST array data to Express gets parsed out as JSON - Stack Overflow

programmeradmin4浏览0评论

My app is Node.js using Express.

Sending this test data from my client using jQuery POST:

{
title: 'hello',
notes: [
{title: 'note 1'},
{title: 'note 2'}
]

}

And this is the result in my server code:

{ title: 'hello', notes: { '0': { title: 'note 1' }, '1': { title: 'note 2' } } }

I want to get the array of notes to insert into my DB as an Array. What am I missing?


As I can't add an answer myself for 8 hours (wtf?) BUT it does not really answer why Express.bodyParser does not parse JSON correctly

Ok I can get it to work by using:

JSON.stringify ( data )

on the client then server side using

JSON.parse( req.rawBody )

This does feel wrong and why does Express.bodyParser not parse JSON correctly?!

My app is Node.js using Express.

Sending this test data from my client using jQuery POST:

{
title: 'hello',
notes: [
{title: 'note 1'},
{title: 'note 2'}
]

}

And this is the result in my server code:

{ title: 'hello', notes: { '0': { title: 'note 1' }, '1': { title: 'note 2' } } }

I want to get the array of notes to insert into my DB as an Array. What am I missing?


As I can't add an answer myself for 8 hours (wtf?) BUT it does not really answer why Express.bodyParser does not parse JSON correctly

Ok I can get it to work by using:

JSON.stringify ( data )

on the client then server side using

JSON.parse( req.rawBody )

This does feel wrong and why does Express.bodyParser not parse JSON correctly?!

Share Improve this question edited May 25, 2023 at 16:07 aynber 23k9 gold badges54 silver badges67 bronze badges asked May 8, 2011 at 13:34 JMWhittakerJMWhittaker 3,6533 gold badges24 silver badges30 bronze badges 2
  • What code you serializing this information on the post, and what code do you use to deserialize it on the server? Ex: JSON.stringify etc – Tejs Commented May 8, 2011 at 13:38
  • I'm using Node.js with Express. It parses any sent body requests using Express.bodyParser. That's all I know I'm new to Node.JS – JMWhittaker Commented May 8, 2011 at 15:08
Add a comment  | 

4 Answers 4

Reset to default 18

On your client:

$.ajax({
  type: 'POST',
  data: JSON.stringify(data),
  contentType: 'application/json',
  url: '/endpoint'
});

On your server:

console.log('POST: ',req.body);

The problem is jQuery mucks around with your data before sending it. If you set the right MIME type, than it leaves you free.

Can you post your client side jQuery code, please? By default jQuery will send data as urlencoded, not JSON. See this question's answer for the way to ensure jQuery sends real JSON data.

FYI the express/connect bodyParser middleware simply uses JSON.parse to parse JSON (and qs.parse to parse urlencoded data). I don't think there are any glaring bugs in those code. Thus I think you should double-check the data you are sending from the browser.

I came across this old question when looking for some other nodejs stuff.

It is a common misconception that jQuery.ajax() functions send data using JSON. Data is sent by jQuery as POST data and not a JSON string. As such all data types (including the numbers in your array) are sent as strings.

This means that express parses the 'array' keys as a string, and since an array can't have a string key in javascript without being an object, it is cast to an object.

It all makes sense then; you can use Express.bodyParser to get a result like that, or you can use JSON.parse or even eval('(' + myPostedData + ')') to get a result object without the indexes.

With your current setup, all you need to do is:

for(var j = 0; j < myVariable.notes.length; j++)
{
    var currentNode = myVariable.notes[j];

    //currentode.title should be 'note 1' for j = 0, etc
}
发布评论

评论列表(0)

  1. 暂无评论