-
Notifications
You must be signed in to change notification settings - Fork 1
/
2-sync-with-xhr.html
33 lines (30 loc) · 1.07 KB
/
2-sync-with-xhr.html
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
<!DOCTYPE html>
<html>
<body>
Search: <input type="text" id="movieTitle"><br>
<button onclick="displayMovie()">Search </button>
<div id="title"></div>
<div id="poster"></div>
<script>
function displayMovie() {
let keyword = document.getElementById("movieTitle").value;
let url = "http://www.omdbapi.com/?t=" + keyword + "&apikey=d9429fd1";
let json = request(url);
let results = JSON.parse(json);
document.getElementById("title").innerHTML = `${results.Title}"`;
document.getElementById("poster").innerHTML = `<img src="${results.Poster}" alt=${results.Title}>`;
}
function request(url) {
let r = new XMLHttpRequest();
r.onreadystatechange = () => {
if (r.readyState == 4 && r.status == 200) {
console.log(r.responseText); //print to console just for fun
}
}
r.open("GET", url, true);
r.send();
return r.responseText;
}
</script>
</body>
</html>