Надеюсь, кто-то может помочь. Целью этого небольшого проекта является поиск в базе данных Movied OMDB и отображение полученных результатов под строкой поиска. У меня такое ощущение, что код ломается, когда я пытаюсь использовать forEach l oop для возвращаемых результатов, но я не могу найти ошибки. Любая помощь приветствуется! Спасибо!
var httpRequest = new XMLHttpRequest();
httpRequest.onload = function() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
var response = JSON.parse(httpRequest.responseText).Search;
var body = document.getElementsByTagName("body");
response.forEach(function (element, index) {
body.appendChild(" <img src="+element[index].Poster+"/>" +
"<p>Title: <a href = 'https://www.imdb.com/title/"+element[index].imdbID+"' >" +element[index].Title+ "</a></p>" +
"<p>Year: "+ element[index].Year+"</p>" +
"<p>Type: "+element[index].Type+"</p>");
});
} else {
console.log(httpRequest.statusText);
}
}
};
httpRequest.onerror = function() {
console.log(httpRequest.statusText);
};
var searchMovie = function () {
var input = document.querySelector('input').value;
if (input) {
httpRequest.open('GET', 'https://www.omdbapi.com/?s=' + input + '&plot=short&apikey=b7da8d63');
httpRequest.send(null);
}
};