I\'m having an issue when sending JSON data from my client to a node server running express.
Here\'s a simple server that demonstrates my issue:
var expr
$.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})}
)