A tiny, dependency-free web page that fetches user data from the JSONPlaceholder public API and displays each userβs name, email, and address. Includes loading state, reload button, and error handling.
- Use JavaScript Fetch API to get user data from a public API.
- Parse the JSON response and display it on the page.
- Handle errors with a
catchblock and offline checks. - Style the output with CSS.
- Add a Reload button to refetch data.
fetch-users/ ββ index.html # Markup: container, status line, reload button, list ββ styles.css # Styling for layout, cards, buttons, messages ββ script.js # Fetch β JSON β render, errors, reload
- Open the folder in VS Code (or any editor).
- Run locally:
- Open
index.htmldirectly in your browser, or - Use the VS Code Live Server extension.
- Open
Open the app:
https://sonydivya.github.io/fetch-users/
- Push this project to GitHub.
- In your repo, go to Settings β Pages.
- Source: Deploy from a branch
Branch:mainΒ· Folder:/(root)β Save - Make a tiny commit to trigger a build (or add an empty file named
.nojekyll) and push. - Check Actions β βPages build and deploymentβ turns green.
- Your live URL will appear in the Deployments β github-pages panel and will follow the pattern above.
Troubleshooting
- If the link doesnβt appear: confirm
index.htmlis at repo root, Pages is set to main / (root), and a Pages build ran successfully in Actions.
- Fetch & display each userβs:
- Name
- Address (street, suite, city, zipcode)
- Loading and status messages
- Reload button to fetch again
- Error handling (offline and HTTP errors)
- Basic HTML escaping of API strings
- Responsive layout using flexbox cards
- Happy path: users load, see βLoaded 10 users.β
- Offline: disable your network β see an offline message.
- Server error: temporarily break the URL to trigger an HTTP error.
- Reload: click Reload to refetch.
fetch(API_URL)βres.json()β loop and render cards..catch()for errors, plusnavigator.onLinecheck.- DOM updated via
innerHTMLon a list container.
- Show more fields (company/phone/website) by editing the card template in
renderUsers. - Swap the API for any public endpoint that returns a similar array.
- Bullets/default fonts β your CSS isnβt loading. Ensure
styles.cssis in the same folder and linked correctly. Hard-refresh (Ctrl/Cmd+Shift+R). - No users β check DevTools (Console/Network) for request errors or CORS blocks.
MIT β use it freely.