i’m writing frontend for some form and im tring to send json data to backend, but here is some trouble
also we have backend on java and there is no allusion that we a getting something
script.js
const signUpBtnForm = document.querySelector('.form__btn_signup')
signUpBtnForm.addEventListener('click', function () {
let xhr = new XMLHttpRequest();
let json = JSON.stringify({
name: document.reg.name,
phone: document.reg.phone,
password: document.reg.password
});
xhr.open("POST", "http://localhost:8080/users");
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
xhr.send(json);
xhr.onload = () => alert(xhr.response);
})
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Форма авторизации</title>
<link rel="stylesheet" href="style.css">
<script src="script.js" defer></script>
</head>
<body>
<article class="container">
<form action="#" name="reg" class="form form_signup" method="post">
<h3 class="form__title"> Регистрация </h3>
<p>
<input type="text" name="name" class="form__input" placeholder="ФИО">
</p>
<p>
<input type="text" name="phone" class="form__input" placeholder="Телефон">
</p>
<p>
<input type="password" name="password" class="form__input" id="password_registration" placeholder="Пароль">
</p>
<p class="password-length">Пароль слишком короткий</p>
<p class="password-includes">В пароли есть недопустимые символы: .@$!%*#?&><)(^-_< /p>
<p>
<input type="password" class="form__input" id="second_password_registration"
placeholder="Подтвердите пороль">
</p>
<p class="check-similar">Пароли не совпадают</p>
<p>
<button class="form__btn form__btn_signup">Зарегистрироваться</button>
</p>
</form>
</article>
</body>
</html>
i think that the trouble is in script, not in connections
may be you have some ideas about that?