<form action="http://localhost:3000/add" method="post" id="myform">
<input type="text" value="" placeholder="username" id="login-name" name="name">
<input type="password" value="" placeholder="password" id="login-pass" name="password">
<button type="submit" class="btn btn-default">ВХОД</button>
</form>
app.post('/add',urlencodedParser,function (req,res) {
console.log(req.body);
});
<form id="myform">
<input type="text" placeholder="username" id="login-name" name="name">
<input type="password" placeholder="password" id="login-pass" name="password">
<input type="button" id="formsub" value="Вход">
</form>
<div id="tester"></div>
<script>
let myButton = document.getElementById('formsub');
myButton.addEventListener('click',function (ev) {
let name = document.forms['myform'].elements['login-name'].value;
let pass = document.forms['myform'].elements['login-name'].value;
let obj = {
name:name,
pass:pass
};
fetch('/add', {method: 'POST',body:JSON.stringify(obj),headers:{'content-type': 'application/json'}})
.then(function (response) {
return response.json();
})
.then(function (data) {
let us = document.createElement('h2');
us.innerHTML = data.name;
let mydiv = document.getElementById('tester');
mydiv.appendChild(us);
})
.catch(alert);
})
</script>
var data = $('#myform').serialize();
$.ajax({
type: "POST",
url: "http://localhost:3000/add?",
data: data,
success: function(msg) {
alert(msg);
}
});