-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
50 lines (41 loc) · 1.02 KB
/
script.js
File metadata and controls
50 lines (41 loc) · 1.02 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
'use strict';
let moviesList = document.getElementById('movies');
/**
* Add poster to page.
* @param {string} posterURL
*/
function addMovies(posterURL) {
let img = document.createElement('img');
img.src = posterURL;
moviesList.appendChild(img);
}
function request(movieTitle) {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open('GET', `http://www.omdbapi.com/?s=${movieTitle}`);
xhr.onload = () => {
if (xhr.status === 200) {
let data = JSON.parse(xhr.responseText);
resolve(data.Search);
} else {
reject(xhr.statusText);
}
};
xhr.onerror = (error) => {
reject(error);
};
xhr.send();
});
}
document.search.onsubmit = (event) => {
event.preventDefault();
moviesList.innerHTML = '';
let movieTitle = event.target.elements.movieTitle.value;
request(movieTitle)
.then((data) => {
data.forEach((movie) => {
addMovies(movie.Poster);
});
})
.catch(error => console.log(error));
};