Skip to content

Latest commit

 

History

History
40 lines (25 loc) · 1.18 KB

README.md

File metadata and controls

40 lines (25 loc) · 1.18 KB

36. Fetch and HTTP

Request data and interpret responses.

Learn

  • fetch
  • Request
  • Response
  • status
  • JSON

How it works

fetch returns a promise for a response. A response can have an unsuccessful HTTP status while the fetch promise still fulfills, so check response.ok. Parsing JSON is another asynchronous step. Keep loading, success, empty, and error UI states distinct.

Common mistake

Treating every fulfilled fetch as success hides 404 and 500 responses.

Guided challenge

Fetch a local JSON file, display a list, and add a clear error state for a missing file.

Run the example

Open example.html in a browser and edit example.js. Save, refresh, and use the Console and debugger to inspect values.

Completion check

  • Explain the core idea in your own words without reading the code.
  • Change one input and predict the output before running it.
  • Test one boundary or failure case, then record what you changed.

Local server

Run python3 -m http.server 8000 from the JavaScripts folder, then open this lesson through http://localhost:8000/. Fetch, modules, storage, history, and workers can behave differently or fail under file URLs.