-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweekEleven.js
More file actions
121 lines (104 loc) · 2.66 KB
/
weekEleven.js
File metadata and controls
121 lines (104 loc) · 2.66 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
// fetch("https://jsonplaceholder.typicode.com/users", {
// headers: {
// Accept: "application/json",
// },
// })
// .then(response => {
// console.log(response);
// if (!response.ok) {
// throw new Error(response.status);
// }
// return response.json();
// })
// .then(elma => {
// // Data handling
// console.log(elma);
// })
// .catch(error => {
// // Error handling
// console.log(error);
// });
// console.log("object");
// const fetchUsersBtn = document.querySelector(".btn");
// const userList = document.querySelector(".user-list");
// fetchUsersBtn.addEventListener("click", () => {
// fetch("https://jsonplaceholder.typicode.com/users")
// .then((response) => {
// console.log("object1");
// if (!response.ok) {
// throw new Error(response.status);
// }
// return response.json();
// })
// .then((veri) => {
// const markup = veri.map((user) => {
// return `<li>
// <p><b>Name</b>: ${user.name}</p>
// <p><b>Email</b>: ${user.email}</p>
// <p><b>Username</b>: ${user.username}</p>
// <p><b>Company</b>: ${user.company.name}</p>
// </li>`;
// })
// .join("");
// userList.insertAdjacentHTML("beforeend", markup);
// })
// .catch((error) => console.log(error));
// });
// console.log("object2");
//GET METHODU
fetch("https://jsonplaceholder.typicode.com/posts")
.then(response => response.json())
.then(data => {
console.log("data:",data);
})
.catch(error => {
console.log("Hata oluştu",error);
})
//POST METHODU
const yeniGonderi = {
title :"merhaba GoIT",
body:"yeni veri yolladım",
userId:"1"
}
fetch("https://jsonplaceholder.typicode.com/posts",{
method:"POST",
headers:{
"Content-Type": "application/json"
},
body:JSON.stringify(yeniGonderi)
})
.then(response => response.json())
.then(data => {
console.log("yeni veri oluştu",data);
})
.catch(error=> {
console.log(error);
})
//PUT METHODU
const guncelGonderi = {
id: 1,
title:"güncel başlık",
body:"bu içerik güncellendi",
userId:"2"
}
fetch(`https://jsonplaceholder.typicode.com/posts/${guncelGonderi.id}`,{
method:"PUT",
headers:{
"Content-Type": "application/json"
},
body:JSON.stringify(guncelGonderi)
})
.then(response => response.json())
.then(data => {
console.log("güncel veri oluştu",data);
})
.catch(error=> {
console.log(error);
})
//DELETE METHODU
fetch("https://jsonplaceholder.typicode.com/posts/1",{
method:"DELETE",
headers:{
"Content-Type": "application/json"
},
})