I am trying to send data with AJAX to node express, but as I handled it without express module (with if (req.method="POST"){function on data...})
as I can catch it with express.
NODE code to catch data:() for now nothing happens even in console.log)
app.get('/getdata',function(req, res){
res.send('Something');
});
app.post('/getdata', function (req, res){
console.log(req.body.objectData);
res.contentType('json');
res.send({ some: JSON.stringify({response:'json'}) });
});
app.listen(process.env.PORT || 5073);
A HTML page with AJAX call:
$(document).ready(function () {
$('#Send').on('click', function () {
var toSend = $('#Sth').val();
alert(toSend);
$.ajax({
type: "POST",
dataType: "json",
data: { objectData: toSend },
contentType: "application/json",
cache: false,
url: 'http://127.0.0.1:5073/'
});
});
});
<body>
<input id="Sth" type="text" name="Content" />
<div id="select_div"><a href="#" id="select_link">Test</a></div>
<div id="test"> Content to be copied</div>
<input id="Send" type="submit" />
</body>
Well, I do not know what to do, please help.
Care to give this a shot?
app.post('/endpoint', function(req, res){
var obj = {};
console.log('body: ' + JSON.stringify(req.body));
res.send(req.body);
});