-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
51 lines (43 loc) · 1.77 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display Book Image</title>
</head>
<body>
<!-- Button to trigger the API call -->
<button onclick="fetchBookById()">Fetch Book Image</button>
<!-- Container to display the image -->
<div id="imageContainer">
<!-- Image will be displayed here -->
</div>
<script>
// Function to fetch book by ID
async function fetchBookById() {
const bookId = '667be11ad608bd246873c836'; // Replace with your actual book ID
const apiUrl = `http://localhost:6969/api/v1/books/${bookId}`; // Replace with your actual API endpoint
try {
// Fetch book data from the API
const response = await fetch(apiUrl);
console.log(response);
const responseData = await response.json();
console.log(responseData);
// Extract image data from the response
const imageData = responseData.data.image.url;
console.log(imageData);
// Create a new image element
const imgElement = document.createElement('img');
// Set the image source to the image cloudinary URL
imgElement.src = imageData;
// Clear existing image if any
document.getElementById('imageContainer').innerHTML = '';
// Append the image element to the container in your HTML
document.getElementById('imageContainer').appendChild(imgElement);
} catch (error) {
console.error('Error fetching book:', error);
}
}
</script>
</body>
</html>