No matter what combination of req.params/req.query/req.body and $.post({..} I use, I am unable to extract JSON data from my Jquery post request in express.
This is my jQuery:
$.post("/mail", {email : "s@gmail.com", name : "s@gmail.com"});
I have also tried various permutations of:
$.ajax({type: "POST", url: "/mail", dataType :"json", data :
{email : "s@gmail.com", name : "s@gmail.com"}});
This is my node.js express code:
app.post("/mail", function(req, res) {
console.log(req.body.name);
console.log(req.route);
console.log("params1: " + req.param.params);
....
I have tried endless ways of accessing the data email and name without success. I'd be surprised if the solution were a simple modification to req.body/ req.params but anything is possible! The data just ain't there! (It does not appear in my url either -- all I see is localhost:1337)
req.body / req.params / req.param.params / req.params all return undefined and req.query returns [].
req.route returns:
{ path: '/mail',
method: 'post',
callbacks: [ [Function] ],
keys: [],
regexp: /^\/mail\/?$/i,
params: [] }
I've tried adding:
app.use(express.bodyParser());
But, admittedly, I do not fully understand its purpose. Any help is greatly appreciated.
Happy holidays! // Sam