Skip to content

Commit 9d04c9c

Browse files
fetch method arguments (POST)
1 parent c8de1d0 commit 9d04c9c

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

fetchApi/script.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Fetching the data.
2+
3+
const url = `https://jsonplaceholder.typicode.com/todos`
4+
const fetchData = async ()=>{
5+
try {
6+
const response = await fetch(url);
7+
if(!response.ok){console.log("Error with the source")}
8+
const data = await response.json();
9+
10+
console.log(data[0].title);
11+
} catch (error) {
12+
console.log(error);
13+
}
14+
}
15+
16+
fetchData()
17+
18+
19+
// post data using the fetch method
20+
21+
const postData = async ()=>{
22+
try {
23+
const response = await fetch("https://jsonplaceholder.typicode.com/todos",
24+
25+
{
26+
method:'POST',
27+
body:JSON.stringify({
28+
title:'Welcome'
29+
}),
30+
headers:{
31+
'Content-Type':'application/json'
32+
}
33+
})
34+
const data = await response.json()
35+
console.log(data)
36+
} catch (error) {
37+
console.log(error)
38+
}
39+
}
40+
41+
postData();

0 commit comments

Comments
 (0)