Answer the question
In order to leave comments, you need to log in
Sending a form to the server and getting data back?
Sending the form to the server:
<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);
});
Answer the question
In order to leave comments, you need to log in
Managed to do it this way:
<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>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question