Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
144 lines (123 loc) · 4.77 KB
/
Copy pathscript.js
File metadata and controls
144 lines (123 loc) · 4.77 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
(function() {
const example = document.getElementById('example');
const cw1 = document.getElementById('cw1');
const cw2 = document.getElementById('cw2');
const cw3 = document.getElementById('cw3');
const answer = document.getElementById('answer');
example.addEventListener("click", function() {
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(array => {
console.log(array);
answer.textContent = JSON.stringify(array);
});
});
cw1.addEventListener("click", function () {
answer.innerHTML = "<p><em>Loading...</em></p>";
setTimeout(() => {
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(posts => {
console.log(posts);
let html = `
<table style="width: 80%; margin: auto; border-collapse: collapse; text-align: center;">
<tr style="background-color: #f0f0f0;">
<th>ID</th>
<th style="color: red;">Tytuł</th>
<th style="color: green;">Treść</th>
</tr>
`;
posts.forEach(post => {
html += `
<tr>
<td style="border: 1px solid #ccc; padding: 8px;">${post.id}</td>
<td style="border: 1px solid #ccc; padding: 8px; color: red;">${post.title}</td>
<td style="border: 1px solid #ccc; padding: 8px; color: green;">${post.body}</td>
</tr>
`;
});
html += "</table>";
answer.innerHTML = html;
})
.catch(error => {
console.error("Błąd pobierania danych:", error);
answer.innerHTML = "<p style='color:red;'>Nie udało się pobrać danych.</p>";
});
}, 1000);
});
cw2.addEventListener("click", function () {
const selectedId = document.getElementById("postSelect").value;
answer.innerHTML = "<p><em>Loading...</em></p>";
fetch(`https://jsonplaceholder.typicode.com/posts/${selectedId}`)
.then(response => response.json())
.then(post => {
console.log(post);
const html = `
<table style="width: 60%; margin: auto; border-collapse: collapse; text-align: center;">
<tr style="background-color: #f0f0f0;">
<th>ID</th>
<th style="color: red;">Tytuł</th>
<th style="color: green;">Treść</th>
</tr>
<tr>
<td style="border: 1px solid #ccc; padding: 8px;">${post.id}</td>
<td style="border: 1px solid #ccc; padding: 8px; color: red;">${post.title}</td>
<td style="border: 1px solid #ccc; padding: 8px; color: green;">${post.body}</td>
</tr>
</table>
`;
answer.innerHTML = html;
})
.catch(error => {
console.error("Błąd:", error);
answer.innerHTML = "<p style='color:red;'>Nie udało się pobrać posta.</p>";
});
});
cw3.addEventListener("click", function () {
const title = prompt("Podaj tytuł nowego posta:");
const body = prompt("Podaj treść nowego posta:");
if (!title || !body) {
answer.innerHTML = "<p style='color:red;'>Musisz wpisać tytuł i treść posta!</p>";
return;
}
answer.innerHTML = "<p><em>Processing...</em></p>";
setTimeout(() => {
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8'
},
body: JSON.stringify({
title: title,
body: body,
userId: 1
})
})
.then(response => response.json())
.then(data => {
console.log("Odpowiedź serwera:", data);
answer.innerHTML = `
<p style="color:green; text-align:center;">
Dodano nowy post o ID = ${data.id}
</p>
<table style="width: 60%; margin: auto; border-collapse: collapse; text-align: center;">
<tr style="background-color: #f0f0f0;">
<th>ID</th>
<th style="color: red;">Tytuł</th>
<th style="color: green;">Treść</th>
</tr>
<tr>
<td style="border: 1px solid #ccc; padding: 8px;">${data.id}</td>
<td style="border: 1px solid #ccc; padding: 8px; color: red;">${data.title}</td>
<td style="border: 1px solid #ccc; padding: 8px; color: green;">${data.body}</td>
</tr>
</table>
`;
})
.catch(error => {
console.error("Błąd wysyłania:", error);
answer.innerHTML = "<p style='color:red;'>Nie udało się dodać posta.</p>";
});
}, 1000);
});
})();