-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_json.html
74 lines (62 loc) · 2.25 KB
/
get_json.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
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
//callback when page load has completed
$(document).ready(function(){
//button handler
$("button").click(function(){
$.ajax({
//TODO replace the id to your own value when you put some content at http://myjson.com
url:"https://api.myjson.com/bins/gu8q2",
//sucess is a function that is called if the fetch of the data went ok
success: function(data, textStatus, jqXHR){
//check the console output in inspector to view the JSON data returned or any errors
console.log(data)
var outputDiv = document.getElementById('outputDiv');
//create a table to format the output the JSON data
var table = document.createElement('table');
//create a header row
header = table.createTHead();
var headerRow = header.insertRow();
headerRow.innerHTML =
'<th><b>Id</b></th>' +
'<th><b>Language</b></th>' +
'<th><b>Edition</b></th>' +
'<th><b>Author</b></th>'
//iterate over each book returned
for (var i = 0; i < data.book.length; i++) {
var tr = document.createElement('tr');
var td = document.createElement('td');
var text = document.createTextNode(data.book[i].id);
td.appendChild(text)
tr.appendChild(td)
td = document.createElement('td');
text = document.createTextNode(data.book[i].language);
td.appendChild(text)
tr.appendChild(td)
td = document.createElement('td');
text = document.createTextNode(data.book[i].edition);
td.appendChild(text)
tr.appendChild(td)
td = document.createElement('td');
text = document.createTextNode(data.book[i].author);
td.appendChild(text)
tr.appendChild(td)
table.appendChild(tr)
outputDiv.appendChild(table)
}
}
});
});
});
</script>
</head>
<body>
<div id="div1">
<h2>Let jQuery AJAX and JSON Test</h2>
<div id="outputDiv"></div>
</div>
<button>Get External JSON Content</button>
</body>
</html>