-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage03.html
129 lines (124 loc) · 5.86 KB
/
page03.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Petfinder API | Page 3</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="css/styles.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#defaultNavbar1"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
<a class="navbar-brand" href="index.html">Home</a></div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="defaultNavbar1">
<ul class="nav navbar-nav">
<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Menu<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="index.html">Home</a></li>
<li><a href="page01.html">1. API Keys and Making Requests</a></li>
<li><a href="page02.html">2. Cross-Domain Requests & JSONP</a></li>
<li><a href="page03.html">3. The pet.get Method & the DOM</a></li>
<li><a href="page04.html">4. Adding Page Content</a></li>
<li><a href="page05.html">5. Adding Images</a></li>
<li><a href="page06.html">6. Getting a Random Cat</a></li>
<li><a href="page07.html">7. Adding a Button and Event Listener</a></li>
<li><a href="page08.html">8. Conclusion</a></li>
</ul>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
<div class="container-fluid">
<div class="jumbotron">
<h1>The pet.get Method & the DOM</h1>
<p>Using the Petfinder API's pet.get method to add content to an HTML page</p>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-lg-12">
<p>You can refer back to the <a href="https://www.petfinder.com/developers/api-docs/" target="_blank">Petfinder API Documentation</a> for an overview of the avaiable methods. There are methods that provide information about particular shelters, but in our examples, we'll be focusing on the <strong>pet.get</strong> and p<strong>pet.getRandom</strong> methods.</p>
<p>Now that our page has access to the API’s JSON data, let’s work with the API’s pet.get method to populate an HTML table with information about this cat. First, let’s examine the structure of the JSON data, to get a better idea of how to access the objects we’ll need. If we go back to the JSONP function on the previous page, we can alter the getKitty function so that it returns the entire JSON entry for this cat, instead of just the name.</p>
<pre>function requestJSONP(url) {
var script = document.createElement('script');
script.src = url;
document.getElementsByTagName('head')[0].appendChild(script);
}
function getKitty(data) {
console.log(data);
} </pre><br/>
<img src="images/json.png" class="img-responsive" alt="JSON Example"><br/>
<p>As you can see below, the information that we want to access is nested within the petfinder.pet object. It will be helpful to have a variable that takes care of those first two levels.</p>
<pre>var response = data.petfinder.pet; </pre>
<p>Let's make sure that our HTML page has a place to put the data. Here, we'll use a table.</p>
<div class="row">
<div class="col-lg-6"><table>
<tbody>
<tr>
<th scope="col"><h3>Cat of the Day</h3></th>
<th scope="col"> </th>
</tr>
<tr>
<td><p>Name:</p></td>
<td id="name"> </td>
</tr>
<tr>
<td><p>Age:</p></td>
<td id="age"> </td>
</tr>
<tr>
<td><p>Sex:</p></td>
<td id="sex"> </td>
</tr>
<tr>
<td><p>Breed:</p></td>
<td id="breed">
<ul id = "breedlist"></ul>
</td>
</tr>
<tr>
<td><p>City:</p></td>
<td id="city"> </td>
</tr>
</tbody>
</table></div>
<div class="col-lg-6"><iframe width="100%" height="300" src="//jsfiddle.net/brennemo/g3nw0rbd/1/embedded/html/" allowfullscreen="allowfullscreen" frameborder="0"></iframe></div>
</div>
</div>
</div>
<hr>
<div class="row">
<div class="col-lg-4">
<a href="page02.html"><button type="button" class="btn btn-info">Prev.</button></a>
<a href="page04.html"><button type="button" class="btn btn-info">Next</button></a>
</div>
</div>
<hr>
<div class="row">
<div class="text-center col-md-6 col-md-offset-3">
<p>Copyright © Morgan Brenner · 2016 </p>
</div>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>