-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
73 lines (66 loc) · 2.02 KB
/
script.js
File metadata and controls
73 lines (66 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Seleciona os elementos
const formulario = document.querySelector("form");
const Inome = document.querySelector(".nome");
const Iemail = document.querySelector(".email");
const Isenha = document.querySelector(".senha");
const Icel = document.querySelector(".cel");
const mostrarSenhaBtn = document.querySelector(".mostrar-senha-btn");
function cadastrar() {
// Verifica se todos os campos estão preenchidos
if (Inome.value === "" || Iemail.value === "" || Isenha.value === "" || Icel.value === "") {
alert("Por favor, preencha todos os campos.");
return;
}
// Envia uma requisição para o endpoint com dados do formulario
fetch("http://localhost:8080/usuarios", {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "POST",
body: JSON.stringify({
nome: Inome.value,
email: Iemail.value,
senha: Isenha.value,
celular: Icel.value
})
})
.then(function (res) {
// Verificação de rede
if (res.ok) {
return res.json();
}
throw new Error('Resposta da rede não estava ok.');
})
.then(function (data) {
console.log(data);
})
.catch(function (error) {
// Trata erros que podem ocorrer no fetch
console.error('Houve um problema com a operação de fetch:', error);
});
}
// Limpar campos do formulario
function limpar() {
Inome.value = "";
Iemail.value = "";
Isenha.value = "";
Icel.value = "";
}
// Visualizar senha
function MostrarSenha() {
if (Isenha.type === "password") {
Isenha.type = "text";
mostrarSenhaBtn.textContent = "🔓";
} else {
Isenha.type = "password";
mostrarSenhaBtn.textContent = "🔒";
}
}
// Eventos do formulario
formulario.addEventListener("submit", function (event) {
event.preventDefault();
cadastrar();
limpar();
});
mostrarSenhaBtn.addEventListener("click", MostrarSenha);