-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtypescript.txt
143 lines (100 loc) · 2.98 KB
/
typescript.txt
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
>>>>> TypeScript <<<<<<<<
---------------------------------------
---------------------------------------
Search Keys:
fetch()
---------------------------------------
---------------------------------------
---------------------------------------
----------------------------------------------------------------------
JavaScript:
In JavaScript, the fetch() method is used to make asynchronous requests to the server and load the information that is returned by the server onto the web pages.
Syntax:
fetch(URL, options);
Code:
fetch('https://reqres.in/api/products')
.then(res => res.json())
.then(data => console.log(data))
.error(e => console.error(e))
.............................................
Init (optional): Any further options such as:
Method: The request method is either GET or POST.
Headers
Body: The body can be any of the following: Body.array.Buffer(), Body.Blob(), Body.formData(), Body.json(), Body.text().
Mode
Credentials
Cache
.........................
Example:
fetch('url', {
Method: 'POST',
Headers: {
Accept: 'application.json',
'Content-Type': 'application/json'
},
Body: body,
Cache: 'default'
})
....................................
GetImage().catch(error => {
console.error(error);
});
async function GetImage(){
const response = await fetch('/anim.jpg');
const blob = await response.blob();
document.getElementById('flower').src=URL.createObjectURL(blob);
}
....................................
getData();
async function getData(){
const response= await fetch('https://www.thecocktaildb.com/api/json/v1/1/search.php?s=margarita%’)
console.log(response);
const data= await response.json();
console.log(data);
length=data.drinks.length;
console.log(data);
var temp="";
for(i=0;i<length;i++)
{
temp+="<tr>";
temp+="<td>"+data.drinks[i].strDrink+"</td>";
temp+="<td>"+data.drinks[i].strInstructions+"</td>";
}
document.getElementById("data").innerHTML=temp;
}
................................
Using fetch() to POST JSON Data:
Object {
title: "",
body: "",
id: 101
}
body: ""
id: 101
title: ""
var form=document.getElementById('form')
form.addEventListener('submit', function(e){
e.preventDefault()
var name=document.getElementById('name').value
var body=document.getElementById('body').value
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: JSON.stringify({
title:name,
body:body,
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
}
})
.then(function(response){
return response.json()})
.then(function(data)
{console.log(data)
title=document.getElementById("title")
body=document.getElementById("bd")
title.innerHTML = data.title
body.innerHTML = data.body
}).catch(error => console.error('Error:', error));
});
-------------------------------------------------------------------------