Published on August 04, 2024By DeveloperBreeze

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Fetch API Example</title>
</head>
<body>
    <h1>User Information</h1>
    <ul id="user-list"></ul>

    <script>
        async function fetchUserData() {
            try {
                const response = await fetch('https://jsonplaceholder.typicode.com/users');
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                const users = await response.json();
                displayUsers(users);
            } catch (error) {
                console.error('Fetch error:', error);
            }
        }

        function displayUsers(users) {
            const userList = document.getElementById('user-list');
            users.forEach(user => {
                const listItem = document.createElement('li');
                listItem.textContent = `${user.name} - ${user.email}`;
                userList.appendChild(listItem);
            });
        }

        fetchUserData();
    </script>
</body>
</html>

Comments

Please log in to leave a comment.

Continue Reading:

Various cURL Examples for API Interactions

Published on January 26, 2024

bash

JSON File Reading and Decoding

Published on January 26, 2024

php

Read JSON Data from a File

Published on January 26, 2024

python

Asynchronous Data Fetching in JavaScript using 'fetch'

Published on January 26, 2024

javascript

Asynchronous Fetch in JavaScript using async/await

Published on January 26, 2024

javascript

JSON Serialization and Deserialization

Published on January 26, 2024

python

JavaScript File Upload using Fetch API and FormData

Published on January 26, 2024

javascript

Fetch JSON Data from API in JavaScript

Published on January 26, 2024

javascript

JavaScript Promise Example

Published on January 26, 2024

php

JavaScript Add Animation to HTML Element

Published on January 26, 2024

javascript