-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTP.js
More file actions
108 lines (93 loc) · 4.23 KB
/
Copy pathHTTP.js
File metadata and controls
108 lines (93 loc) · 4.23 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Функция для скрытия
function hidePreloader() {
const preloader = document.getElementById("preloader");
preloader.style.display = "none";
}
// Функция для выполнения запроса с помощью Fetch API и возврата Promise
function fetchWithPromise(url, options) {
return new Promise((resolve, reject) => {
fetch(url, options).then(response => {
if (!response.ok) {
throw new Error("ошибка HTTP: " + response.status);
}
return response.json();
}).then(data => {
resolve(data);
}).catch(error => {
reject(error);
});
});
}
// Функция для обработки данных и отображения их на странице
function renderData(data) {
const comments = document.getElementById("comments");
// Создаем таблицу
const table = document.createElement("table");
// Создаем заголовок таблицы
const thead = document.createElement("thead");
const headerRow = document.createElement("tr");
const headers = ["имя", "email"]; // Замените заголовки на свои
headers.forEach(headerText => {
const th = document.createElement("th");
th.textContent = headerText;
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
table.appendChild(thead);
const tbody = document.createElement("tbody");
data.slice(0, 5).forEach(item => { // изменяем метод forEach на slice и добавляем параметры начала и конца диапазона
const row = document.createElement("tr");
const cell1 = document.createElement("td");
cell1.textContent = item.name; // заменяем textcontent на textContent
const cell2 = document.createElement("td");
cell2.textContent = item.body; // заменяем textcontent на textContent
row.appendChild(cell1);
row.appendChild(cell2);
tbody.appendChild(row);
});
table.appendChild(tbody); // заменяем appendchild на appendChild
// Очищаем контент и добавляем таблицу
comments.innerHTML = '';
comments.appendChild(table);
}
async function randomData() {
try {
const randomId = Math.floor(Math.random() * 101) + 100; // Генерация случайного ID
const url = "https://jsonplaceholder.typicode.com/comments?id_gte=${randomId}";
const options = {method: 'GET'};
const response = await fetch(url, options);
if (!response.ok) {
throw new Error("Произошла ошибка. Попробуйте позже");
}
const data = await response.json();
renderData(data);
hidePreloader();
} catch (error) {
console.error(error);
const content = document.getElementById("comments");
content.textContent = "Произошла ошибка. Попробуйте позже";
hidePreloader();
}
}
// Ожидание события загрузки страницы
document.addEventListener("DOMContentLoaded", () => { // Выполнение запроса при загрузке страницы
const url = "https://jsonplaceholder.typicode.com/comments";
const options = {method: "GET"};
fetchWithPromise(url, options)
.then(data => {
renderData(data);
hidePreloader();
})
.catch(error => {
console.error(error);
const content = document.getElementById("comments");
content.textContent = "Произошла ошибка. Попробуйте позже";
hidePreloader();
});
// Находим кнопку по ее ID
const randomDataButton = document.getElementById("randomDataButton");
// Добавляем обработчик события клика на кнопку
randomDataButton.addEventListener("click", () => {
randomData(); // Выполнение запроса при клике на кнопку
});
});