forked from danielscarvalho/FTT-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaulajs18.html
More file actions
54 lines (40 loc) · 1.58 KB
/
Copy pathaulajs18.html
File metadata and controls
54 lines (40 loc) · 1.58 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
50
51
52
53
54
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Aula 18</title>
<script>
var apiMessage;
function callApi() {
var query = document.getElementById("busca").value;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
apiMessage = JSON.parse(this.responseText); //importa o arquivo texto inteiro em um único string
document.getElementById("jsonData").innerText = this.responseText;
mostraResposta(apiMessage);
}
};
xhttp.open("GET", "https://api.duckduckgo.com/?format=json&pretty=1&q=" + query, true);
xhttp.send();
}
function mostraResposta(resp) {
var firstText = resp.RelatedTopics[0].Text;
var pTag = document.createElement("p");
pTag.innerText = firstText;
document.getElementById("resposta").appendChild(pTag);
}
</script>
<style>
pre { background-color: lemonchiffon;
}
</style>
</head>
<body>
<h1>JavaScript Aula 18</h1>
<h2>JSON, API, AJAX - Asynchronous JavaScript And XML</h2>
<input type="search" id="busca" placeholder="O que você quer saber?" required></input>
<button onclick="callApi();">Get data</button>
<div id="resposta"></div>
<pre id="jsonData"></pre>
</body>
</html>