46 lines
1.2 KiB
HTML
46 lines
1.2 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>Display Posts</title>
|
|
</head>
|
|
<body>
|
|
<h1>List of Posts</h1>
|
|
<ul id="post-list"></ul>
|
|
|
|
<script>
|
|
'use strict';
|
|
|
|
async function displayPosts() {
|
|
const postList = document.querySelector('#post-list');
|
|
|
|
try {
|
|
const response = await fetch('https://dummyjson.com/posts');
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Network response was not ok');
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
data.posts.forEach((post) => {
|
|
const listItem = document.createElement('li');
|
|
listItem.textContent = post.title; // Display the title of each post
|
|
postList.appendChild(listItem);
|
|
});
|
|
} catch (error) {
|
|
console.error('Error fetching posts:', error);
|
|
|
|
const errorItem = document.createElement('li');
|
|
|
|
errorItem.textContent = 'Error loading posts.';
|
|
postList.appendChild(errorItem);
|
|
}
|
|
}
|
|
|
|
displayPosts();
|
|
</script>
|
|
</body>
|
|
</html>
|