I am trying to send some values via a POST
method to a node.js
method.
The node.js
will return an output based on the result and I need to update the HTML.
I am making use of ejs
to communicate back with the HTML. I need to make use of the Javescript method as I will be using it to customize the UI based on the output I received after executing the Node JS method.
HTML
<form id="form-register" method="post" enctype="multipart/form-data">
<input type="logintext" value="" name="nam" class="nameC" id="mc" >
<input type="submit" name="subscribe" onclick="myFunction()" id="mc-submit-number">
<label >REEEEE <%= titles %></label>
</form>
JAVASCRIPT METHOD
function myFunction() {
$("#mc-form-register").submit(function(e) {
e.preventDefault();
//Prevents the page from refreshing
var $this = $(this);
$.post("http://localhost:8081/ttt",
$this.serialize(), // Serializes form data in standard format
function(data, status) { /** code to handle response **/
alert('Hey ' + data.titles + ' ' + status);
},
"json" // The format the response should be in
);
});
}
NODE JS
app.post('/ttt', function (req,res){
loginProvider.signUp(params, function(err, data) {
if (err) {
res.render('index',{title: 'HOME',titles:err.stack
});
}
else {
res.render('index',{title: 'HOME',titles:'SUCCESS'
});
}
});
}