<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>
function but(id) {
fetch('/ajax', {method: 'POST',body:JSON.stringify({
name:'vlad',
id:id
}),headers:{'content-type': 'application/json'}})
.then(function (response) {
alert(response.status); // 200
return response.json();
})
.then(function (data) {
alert(data.id);
let elem = document.getElementById('h2o');
elem.innerText = data.id;
})
.catch(alert);
}