Fetching data with the Fetch API and displaying it safely
AJAX allows a page to update specific parts without reloading everything, and modern browsers provide the Fetch API, which replaces the older XMLHttpRequest. [S1]
Fetch basics
The Fetch API uses promises and supports async/await, and it has a built-in .json() method for JSON handling. [S1]
The async keyword tells JavaScript that a function might need to wait for things. [S1]
When you use fetch without any extra options, it automatically creates a GET request. [S1]
The lesson applies encodeURIComponent() to safely handle special characters such as # or & in URLs. [S1]
Showing results
The textContent property is the recommended way to display user data. [S1]
Although innerHTML appears in many tutorials, it can execute embedded scripts. [S1]
Handling failures
An extension that calls an API should handle errors gracefully, showing a message when it could not fetch data for a region. [S2]
API integration means teaching your code how to communicate with other services. [S2]
Claims
- id: C1 claim: "Fetch replaces the older XMLHttpRequest." source: S1 evidence: "which replaces the older"
- id: C2 claim: "fetch without options creates a GET request." source: S1 evidence: "when you use
fetchwithout any extra options, it automatically creates a"
- id: C3 claim: "innerHTML can execute embedded scripts." source: S1 evidence: "it can execute embedded scripts"
- id: C4 claim: "API integration teaches code to communicate with other services." source: S2 evidence: "it's really just teaching your code how to communicate with other services"
Sources
- [S1] Build a Banking App Part 3: Methods of Fetching and Using Data. Microsoft Web Dev for Beginners (GitHub). https://github.com/microsoft/Web-Dev-For-Beginners/blob/main/7-bank-project/3-data/README.md MIT; accessed 2026-11-05.
- [S2] Browser Extension Project Part 2: Call an API, use Local Storage. Microsoft Web Dev for Beginners (GitHub). https://github.com/microsoft/Web-Dev-For-Beginners/blob/main/5-browser-extension/2-forms-browsers-local-storage/README.md MIT; accessed 2026-11-05.
Attribution: source authors named above; adapted summaries, not endorsed by them. MIT notices: Copyright (c) Microsoft Corporation, MIT License.
License: CC-BY-4.0 · Adapted summaries of the named sources below; wording and synthesis by Librarian.
Cite this
Librarian. 2026. "Fetching data with the Fetch API and displaying it safely." iinventedlight Library, iil:note:m5zyhg5a, version of 2026-11-05. https://iinventedlight.com/library/notes/js-fetch-api-display-data/. License: CC-BY-4.0.
@misc{iil_note_m5zyhg5a,
author = {{Librarian}},
title = {{Fetching data with the Fetch API and displaying it safely}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-05},
url = {https://iinventedlight.com/library/notes/js-fetch-api-display-data/},
note = {Permanent ID iil:note:m5zyhg5a. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/js-fetch-api-display-data.md.