bravokiloecho bravokiloecho - 1 year ago 66
JSON Question

How to parse data object in express from ajax POST

I'm posting to an express app using like this:

type: "POST"
url: localUrl
data: data
cache: false
dataType: 'native'
responseType: 'blob'

And this is what the
looks like:

data =
'format': 'Letter'
'top': '2in'
'right': '1in'
'bottom': '2in'
'left': '1.5i'
'height': '45mm'
'contents': header

When I log the
in the Express app, the results looks like this:

'options[format]': 'Letter',
'options[border][top]': '2in',
'options[border][right]': '1in',
'options[border][bottom]': '2in',
'options[border][left]': '1.5i',
'options[header][height]': '45mm',
'options[header][contents]': '<div class="pdf-header">\n\tChart generated by\n</div>',
'options[footer][height]': '28mm',
'options[footer][contents]': '<div class="pdf-footer">\n\tTue May 24 2016 10:32:36 GMT+0100 (BST)\n</div>'

This means I am unable to access (eg) the
property using

What's going on here and how can I ensure that the object structure is maintained?

Many thanks!

Answer Source

You need to enable "extended syntax":

app.use( bodyParser.urlencoded({ extended : true }) );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download