I am trying to make an AJAX post to my Express API.
I am expecting {user:Todd} to be returned in my data object, however whenever i try to log the request body, the object shows as empty./
$.ajax({
url: '/api/test',
type: 'POST',
data: ({user: "Todd"})
})
I always test my endpoints with cURL as well, and i remember that i needed to pass the header "Content-Type: application/json" for this to work in the past.
$.ajax({
url: '/api/test',
type: 'POST',
data: ({user: "Todd"}),
contentType: 'application/json'
})
I am listening for requests on my NodeJS server and the above request doesnt even touch my server, while the first one sends empty JSON.
I am officially confused! How can adding a header break my API like this?
Here is the simple source code to my API.
var express = require('express');
var bodyParser = require('body-parser')
let app = express();
let port = 3000
app.use(bodyParser.json({
limit:'150mb'
}));
app.post('/api/test', function(req, res) {
console.log(req)
console.log(req.body)
});
app.listen(port, () => console.log(`App listening on port ${port}`));