-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchapi.js
57 lines (56 loc) · 1.73 KB
/
fetchapi.js
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
console.log("Fetch API in the js");
// button with id btnone
let myBtn = document.getElementById("btnone");
// below >> div with id contentdata
let content = document.getElementById("contentdata");
// function getData()
// {
// console.log("Started getData Function");
// let url = "atharv.txt";
// // the function runs as the promise inside the resolve
// // the fetch api will run in the background
// fetch(url).then((response)=>{
// console.log("Inside the first then");
// return response.text();
// // after promise complete again dot then
// }).then((data)=>{
// console.log(data);
// console.log("Inside the second then");
// })
// }
// function getData()
// {
// console.log("Started getData Function");
// let url = "https://api.github.com/users";
// // the function runs as the promise inside the resolve
// // the fetch api will run in the background
// fetch(url).then((response)=>{
// console.log("Inside the first then");
// return response.json(); // parse data
// // after promise complete again dot then
// }).then((data)=>{
// console.log(data);
// console.log("Inside the second then");
// })
// }
// console.log("Before running the get data");
// getData();
// console.log("After running the get data");
function postData()
{
let url = "onlineFakejsondatalink";
data = "data in the link";
params = {
method:"post",
headers:{
"Content-Type":"application/json"
},
body:data
}
// the function runs as the promise inside the resolve
// the fetch api will run in the background
fetch(url,params).then(response=>{response.json(); // parse data
// after promise complete again dot then
}).then(data=>{ console.log(data);
});
}