-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise4_ajax.html
119 lines (108 loc) · 3.68 KB
/
exercise4_ajax.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
<!DOCTYPE html>
<html>
<head>
<script src="js/jquery.js"></script>
<meta name="description" content="JQuery Exercise 3: Events" />
<meta charset=utf-8 />
<title>JQuery Exercise 3: Events</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
td,
th {
border: 1px solid;
}
body {
font: 12pt arial, sans-serif;
color: grey;
}
</style>
</head>
<body>
<pre>
/**
* JQuery AJAX Exercise
*
*/
// ******************************************
// Exercise 1
//
// - Perform an ajax request using JQuery to
// the url stored within the var 'url'.
// - The JSON response will contain a weather
// forecast for Brussels. The path
// 'query.results.channel.item.forecast'
// in the result leads to an array of
// forecasts.
// - In the success handler: for each forecast
// in the result, create a table row and add
// it to the table.
// ******************************************
// ******************************************
// Exercise 2
//
// - Add a 'fail' handler to the previous
// ajax request
// - In the fail handler, add '<‍h1>Fail<‍/h1>'
// to the HTML body.
// - Change something in the host of the URL
// to verify that the change handler is
// triggered.
// ******************************************
</pre>
<div id="results">
<table>
<tr>
<th>Date</th>
<th>Low</th>
<th>High</th>
</tr>
</table>
</div>
<script type="text/javascript">
/**
* JQuery AJAX Exercise
*
*/
// ******************************************
// Exercise 1
//
// - Perform an ajax request using JQuery to
// the url stored within the var 'url'.
// - The JSON response will contain a weather
// forecast for Brussels. The path
// 'query.results.channel.item.forecast'
// in the result leads to an array of
// forecasts.
// - In the success handler: for each forecast
// in the result, create a table row and add
// it to the table.
// ******************************************
var url = 'https://samples.openweathermap.org/data/2.5/forecast?id=3337389&appid=d6580bb41f5aad2e865e9df6966c263e';
// ******************************************
// Exercise 2
//
// - Add a 'fail' handler to the previous
// ajax request
// - In the fail handler, add '<h1>Fail</h1>'
// to the HTML body.
// - Change something in the host of the URL
// to verify that the change handler is
// triggered.
// ******************************************
$.ajax(url)
.success(function (data) {
var forecasts = data.list;
for (var i = 0; i < forecasts.length; i++) {
var fc = forecasts[i];
$('table').append('<tr><td>' + new Date(fc.dt).toLocaleDateString() + '</td><td>' + fc.main.temp_min + '</td><td>' + fc.main.temp_max + '</td></tr>');
}
})
.fail(function () {
$('body').append('<h1>Fail</h1>');
});
</script>
</body>
</html>