-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-http-get-request.html
76 lines (67 loc) · 1.78 KB
/
js-http-get-request.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
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!-- HTTP GET Request -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>HTTP GET Request</title>
</head>
<style>
ul {
width: 50%;
margin: 0 auto;
}
li {
width: 100%;
float: left;
list-style: none;
padding: 12px 0px;
border-bottom: 1px solid #ddd;
}
span {
width: 100%;
float: left;
}
img {
width: 100px;
}
</style>
<body>
<h1>IMDB SEARCH</h1>
<input id="input" type="text" placeholder="Film ara">
<button onclick="search()">Ara</button>
<div class="wrapper"></div>
</body>
<script>
const url = 'http://www.omdbapi.com/?apikey=b2c4c7ec&s='
const dummyImage = 'https://www.softwarearge.com/wp-content/uploads/2018/09/no-image-icon-6.png'
let searchInput = 'anatolia'
function httpGet (url) {
let elem = '<ul>'
let xmlHttp = new XMLHttpRequest()
xmlHttp.open( 'GET', url, false )
xmlHttp.send( null )
let data = JSON.parse(xmlHttp.responseText).Search
if (data) {
for (let i = 0; i < data.length; i++) {
if (data[i].Poster !== 'N/A') {
elem += '<li> <img src="'+ data[i].Poster +'" <span>'+ data[i].Title +'</span> <span>'+ data[i].Year +'</span> </li>'
} else {
elem += '<li> <img src="'+ dummyImage +'" <span>'+ data[i].Title +'</span> <span>'+ data[i].Year +'</span> </li>'
}
}
} else {
alert('Data Yok')
}
elem += '</ul>'
document.querySelector('.wrapper').innerHTML = elem
}
function search () {
let value = document.querySelector('#input').value
searchInput = value
httpGet(url + searchInput)
}
httpGet(url + searchInput)
</script>
</html>