POSTing json to express using jQuery
$.post
sends url-encoded data, so what is really sent is number=1
, which then is parsed as well as it can by bodyParser middleware.
To send json you have to use JSON.stringify({number:1})
.
Using $.post
unfortunately won't set the appropriate Content-Type
header (express will handle it anyway), so it's better to use:
$.ajax({ url: '/', type: 'POST', contentType: 'application/json', data: JSON.stringify({number:1})})