Repository navigation
Expand file tree
/
Copy pathindex.php
More file actions
124 lines (94 loc) · 2.95 KB
/
Copy pathindex.php
File metadata and controls
124 lines (94 loc) · 2.95 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<!-- meta -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Javascript playground">
<!-- title -->
<title>JavaScript Developer</title>
<!-- Favicon -->
<link rel="icon" type="image/x-icon" href="assets/images/js-logo.png">
<!-- Custom CSS -->
<link rel="stylesheet" type="text/css" href="css/styles.css">
<!-- Bootstrap CSS -->
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<!-- Donuts -->
<div class="donuts">
<!-- Choco Banana -->
<div class="donut-choco-banana"></div>
<span>Choco Banana Donut</span>
<br>
<!-- Fullberry -->
<div class="donut-fullberry"></div>
<span>Fullberry Donut</span>
<br>
<!-- Oreo -->
<div class="donut-oreo"></div>
<span>Oreology</span>
</div>
<!-- /.donuts -->
<!-- Load data button. -->
<div class="btn-center">
<button id="loadDataButton" class="btn btn-primary mb-3">Load Data</button>
<!-- Loading animation -->
<div class="loader"></div>
</div>
<div id="dataContainer" class="mt-4">
<!-- Data from AJAX will be loaded here. -->
</div>
</div>
<!-- /.container -->
<script type="text/javascript" src="js/jquery-3.6.0.min.js"></script>
<script type="text/javascript">
// Ensure that this code only run after DOM (Document Object Model) fully loaded.
$(document).ready(function() {
// Add click event listener to the button with id "loadDataButton".
$("#loadDataButton").click(function() {
// Show loader when AJAX begins.
$(".loader").show();
// Reset content on load.
$("#dataContainer").html("");
// Perform an AJAX GET request to fetch data from API.
$.ajax({
url: "https://dummyjson.com/posts", // URL to fetch.
method: "GET", // HTTP method.
// If response succesfull.
success: function(response) {
// Hide loader.
$(".loader").hide();
// Check if the data received is an array.
if (Array.isArray(response.posts)) {
// Create a variable to save the post contents.
let content = "";
// Iterate through each post in the array.
response.posts.forEach(function(post) {
// Append each post's title and body to the variable.
content = `<div class="post">
<h3>${post.title}</h3>
<p>${post.body}</p>
</div>`;
});
// Display the content in "dataContainer".
$("#dataContainer").html(content);
} else {
// Display a message if no post data found.
$("#dataContainer").html("No posts found");
}
},
// If the operation failed.
error: function() {
// Hide the loader.
$(".loader").hide();
// Display an error message.
$("#dataContainer").html("An error occured while loading data.");
}
});
});
});
</script>
</body>
</html>