I am making an Ajax request that looks like this:
$.ajax({
url: '/gen',
type: 'POST',
data: JSON.stringify({'one': 1, 'two':2}),
success: function(data) {console.log(this)}
});
and my express portion looks like this:
var express = require('express');
var app = express();
var router = express.Router();
app.set('port', (process.env.PORT || 5000));
router.post('/gen', function(req, res) {
console.log(req.body);
});
this always outputs undefined in the console.
How can I change this around to make the req.body, or any part of the req, contain the information I am trying to send over to the express portion of the code.
You need to use the body parser.
var bodyParser = require('body-parser')
app.use(bodyParser.json());
See:
You may also need to add:
contentType: 'application/json',
in your .ajax() options.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With