-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
67 lines (57 loc) · 1.73 KB
/
Copy pathscript.js
File metadata and controls
67 lines (57 loc) · 1.73 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
async function login() {
const urlLogin = "http://137.184.108.252:5000/api/login";
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: "324124616@ulife.com.br",
password: "Kxr5XccRdFIt",
}),
};
const response = await fetch(urlLogin, options); //requisição para o servidor
const jsonResponse = await response.json();
if (!response.ok) {
console.error("Error:", jsonResponse);
return null;
} else {
return (token = jsonResponse.token);
}
}
async function getCidades() {
try {
const token = await login(); //chama método login para obter o token
let tokenAtual = document.getElementById("token");
tokenAtual.style.color = "red";
tokenAtual.innerHTML = token; //exibe o token na tela
const response = await fetch("http://137.184.108.252:5000/api/cidades", {
method: "GET",
headers: {
"Content-Type": "application/json",
"Cache-Control": "no-cache",
Accept: "*/*",
"Accept-Encoding": "gzip,deflate",
Connection: "keep-alive",
"x-access-token": token,
},
});
const cidades = await response.json();
if (!response.ok) {
console.error("Error:", cidades);
return;
} else {
const table = document.getElementById("tabela-corpo");
cidades.forEach((item) => {
const linha = table.insertRow();
const coluna1 = linha.insertCell(0);
const coluna2 = linha.insertCell(1);
coluna1.innerHTML = item.id;
coluna2.innerHTML = item.nome;
});
}
} catch (error) {
console.error("Error fetching data:", error);
}
}
getCidades(); //chama a função para obter as cidades