I am passing variable from AJAX to Node JS server but, when trying to extract the data from the request I keep getting [Object Object]. How would I get the data passed from AJAX to Node ?
AJAX:
$.ajax({
type: "POST",
url: "/Watch",
data: {"name" : stockName},
contentType: "json",
success: function(d) {
//Stuff happening
},
error: function(d) {
console.log("Error");
}
});
Node JS Server:
app.post('/Watch', function(req, res) {
console.log("DATA from AJAX = " + req.body);//Returns [Object Object]
console.log("DATA from AJAX = " + req.body.data);//Returns 'undefined'
console.log("DATA from AJAX = " + req.data);//Returns 'undefined'
console.log("DATA from AJAX = " + req.name);//Returns 'undefined'
console.log("DATA from AJAX = " + req.body.data);//Returns 'undefined'
res.send("");
});
In the Node JS the console.log shows some of the different thing I tried to get the data from the request.
app.use(bodyParser.json())or something similar added before this route handler?JSON.stringify()yourdataand use a fullcontentTypelike 'application/json'.req.body.namefor example.