-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
35 lines (31 loc) · 1.2 KB
/
script.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
const searchForm = document.querySelector('#search-form');
const movie = document.querySelector('#movies')
function apiSearch(event){
event.preventDefault();
const searchText = document.querySelector('.form-control').value;
const server = 'https://api.themoviedb.org/3/search/multi?api_key=fe1ddbf26c8c5d6d90244797b03f0315&language=ru&query=' + searchText;
requestApi('GET', server);
}
searchForm.addEventListener('submit', apiSearch);
function requestApi(method, url){
const request = new XMLHttpRequest();
request.open(method, url)
request.send();
request.addEventListener('readystatechange', function() {
if (request.readyState !== 4) return;
if(request.status !== 200){
console.log('Error: ' + request.status);
return;
}
const output = JSON.parse(request.responseText);
let inner = '';
output.results.forEach(function (item){
let nameItem = item.name || item.title;
let dateItem = item.first_air_date || item.release_date;
//inner += '<div class="col-5">' + nameItem + '</div>';
inner += `<div class="col-15 col-md-4 col-xl-3">${nameItem} - ${dateItem}</div>`;
});
movie.innerHTML = inner;
console.log(output);
});
}