I am trying to send a POST request with JSON data from angularjs to node/express, but all the data is showing up in the KEY of the req.body instead of key value pairs.
I saw this question with the same issue, but the solution did not fix my problem: Express.js : POST data as KEY of a req.body object instead of VALUE of req.body
Here is my client side code (ANGULARJS):
var job = {
session_id: $scope.uid,
timestamp: 0,
invoice_id: $params.i
};
var url = 'https://example/' + $scope.uid;
var config = {
headers : {
'Content-Type' : 'application/json; charset=UTF-8'
}
};
//var config = {};
$http.post(url, job, config).then(
function successCallback(response) {
$scope.loading = false;
console.log('Success:', response.data)
}, function errorCallback(response) {
$scope.loading = false;
console.log('Error:', response.data)
});
Here is my server side code (NODEJS/EXPRESS):
app.use(bodyParser.json());
app.use(function (req, res, next) {
var domainWhitelist = ['https://www.example.com'];
var origin = req.headers.origin;
if(domainWhitelist.indexOf(origin) > -1){
res.setHeader('Access-Control-Allow-Origin', origin);
}
res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
next();
});
app.post('/payment/:session', function(req, res) {
var data = req.body;
console.log('data:', data)
});
This is the data I get on the server side:
{'{"session_id":"123","timestamp":0,"invoice_id":"-test"}': '' }