-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAJAXandFetchAPI.js
More file actions
49 lines (42 loc) · 1.12 KB
/
AJAXandFetchAPI.js
File metadata and controls
49 lines (42 loc) · 1.12 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
var request = new XMLHttpRequest();
request.open('GET', '/', /* async = */ false);
request.send();
console.log(request.responseText.substring(0, 150));
console.log('...');
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
function getPosts() {
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest();
req.open('GET', 'https://jsonplaceholder.typicode.com/posts');
req.onload = function() {
if (req.status == 200) {
resolve(JSON.parse(req.response));
}
else {
reject();
}
};
req.send();
})
}
getPosts().then(r =>{
console.log(r);
}).catch(() => {
console.log('Algo salió mal');
});
fetch('http://example.com/movies.json')
.then((response) => {
return response.json();
})
.then((myJson) => {
console.log(myJson);
});