-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvanilla.js
More file actions
60 lines (46 loc) · 1.89 KB
/
vanilla.js
File metadata and controls
60 lines (46 loc) · 1.89 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
// create HTML via DOM manipulation
const app = document.getElementById('app');
const logo = document.createElement('img');
logo.src = 'images/logo.png';
app.appendChild(logo);
const container = document.createElement('div');
container.setAttribute('class', 'container');
app.appendChild(container);
// create a new HTTP request to retrieve the list of films
let request = new XMLHttpRequest();
// opens the request, but does not send it.
request.open('GET', 'https://ghibliapi.herokuapp.com/films', true);
// must be below the 'request.open()' method
// this allows us to access the data retrieved from the API endpoint
request.onload = function() {
let data = JSON.parse(this.response);
if (request.status >= 200 && request.status < 400) {
data.forEach(movie => {
const card = document.createElement('div');
card.setAttribute('class', 'card');
const header = document.createElement('h1');
header.textContent = movie.title;
const p = document.createElement('p');
p.textContent = `${movie.description.substring(0, 500)}...`;
container.appendChild(card);
card.appendChild(header);
card.appendChild(p);
});
} else {
console.log('Error: ' + this.statusText);
const card = document.createElement('div');
card.setAttribute('class', 'card');
const header = document.createElement('h1');
header.textContent = 'Oh, no! Something went wrong!';
const p = document.createElement('p');
p.textContent = 'An error occurred: '
+ this.responseURL + ' - '
+ this.status + ' - ('
+ this.statusText + ').';
container.appendChild(card);
card.appendChild(header);
card.appendChild(p);
}
}
// sends the request to retrieve the data (see 'Network' tab in dev tools)
request.send();