I have HTML inputs in my index.html file.
<input type="text" id="handle" >
<input type="text" id="message" >
<button id="send">send</button>
When I fill up data and click send, I want to send them to my node script where I can do something with passed data.
My index.html's script:
$("#send").on("click", function() {
var message = $("#message").val();
var handle = $("#handle").val();
var xhr = new XMLHttpRequest();
var data = {
param1: handle,
param2: message
};
xhr.open('POST', '/data');
xhr.onload = function(data) {
console.log('loaded', this.responseText);
};
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(data));
});
And how I have tried to recieve data in my serverside test.js file.
app.post('/data', function(req, res){
var obj = {};
console.log('body: ' + req.body);
res.send(req.body);
});
In this case, the output shows: body: undefined
How can I send the data from my client side pages to server side so that I can use them to perform my other operations?