jquery - express4 bodyparser post array item is udefined -


i have express application uses bodyparser json content post request. if write out content of whole body looks this:

{   'device[group]': 'testgroup',   'device[name]': 'testname',   'events[http][address]': 'http://192.168.77.11/api' } 

immediately after write out content of events gives me undefined. doing wrong?

my code follows:

app.post('/settings', function(req, res) {     console.log(req.body);     console.log(req.body.events); // undefined 

client side code:

$.ajax({             url: posturl,             data: {                     "device": {                         "group": $('#devicegroup').val(),                         "name": $('#devicename').val()                     },                     "events": {                         "http": {                             "address": $('#httpaddress').val()                         }                     }             },             type: 'post',             datatype: 'json'         }).success(function(response) {             console.log(response);         }); 

you need send data string, not plainobject:

$.ajax({             url: posturl,             data: json.stringify( {                     "device": {                         "group": $('#devicegroup').val(),                         "name": $('#devicename').val()                     },                     "events": {                         "http": {                             "address": $('#httpaddress').val()                         }                     }             } ),             type: 'post',             contenttype: 'application/json',             datatype: 'json'         }).success(function(response) {             console.log(response);         }); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -