# Fetch API and JSON

> How a page asks a server for data without loading a new page. Using fetch with await, reading JSON, and checking that the reply was not an error.

**Course:** [HTML, CSS and JavaScript](https://learn.benibix.com/learn/development-web)  
**Time:** 13 min

**After this lesson you can:**

- Request data with fetch and await
- Read a JSON reply into JavaScript
- Check the status before using the data

## How a page gets data from a server

The Fetch API is how code in a web page asks a server for data. You call fetch() with an address, and it sends the request while the page keeps working. The reply usually comes back as JSON. A reply with status code 200 to 299 is a success, and response.ok is true.

A fetch takes time, from a blink to several seconds. JavaScript does not stop and wait. fetch() hands back a promise, which stands for a result not ready yet. Writing await in front of it pauses only that one function until the reply comes. Clicks and scrolling go on.

JSON is a plain text format for data made of names and values. It looks like {"temp": 18, "city": "London"}. The names are always in double quotes. Calling response.json() reads that text and turns it into a normal JavaScript object, so data.temp gives you 18.

Every reply carries a status code, a three-digit number that says how the request went. 200 means it worked. 404 means the address was not found, and 500 means the server broke. The surprise is that fetch counts a 404 or a 500 as a reply, not a failure.

So fetch only throws an error when the request never got a reply at all, such as when the network is down. A 404 page comes back quietly and your code reads it as if it were the data. Always check response.ok, and stop when it is false.

A page can also send data. Give fetch a second input with method: "POST" and a header that says the body is JSON. Then add the body, turned into JSON text. The server reads it much like a form, and the page does not reload.

People often call fetch on a different site and are surprised when it is blocked. Browsers stop a page from reading another site's replies unless that site says yes. The usual fix is to call your own server, and let it call the other site.

```javascript
async function showWeather() {
  const response = await fetch("/api/weather?city=London");
  if (!response.ok) {
    throw new Error("Request failed: " + response.status);
  }
  const data = await response.json();   // { "temp": 18 }
  document.querySelector("#temp").textContent = data.temp + "°C";
}
```
Ask, check the status, then read the JSON. Skipping the check treats an error page as data.

![A web page in the browser runs fetch("/api/weather"). An arrow marked request goes to a server, which holds { "temp": 18 }. An arrow marked response, JSON, comes back, and the page shows 18°C in London.](https://learn.benibix.com/diagrams/dev-fetch.svg)

### Example: Jack's app shows undefined degrees

Jack's weather page sometimes shows undefined°C. The server returns a 404 for a city it does not know, and his code reads the error page as data.

He adds a check on response.ok. Now an unknown city shows a clear message, and only a 200 reply reaches the screen.

**Words in this lesson:** [Fetch API](https://learn.benibix.com/term/development/fetch-api), [Async/Await](https://learn.benibix.com/term/development/async-await), [JavaScript Object Notation (JSON)](https://learn.benibix.com/term/development/json), [HTTP Status Code](https://learn.benibix.com/term/infrastructure/http-status-code)

**Question:** Does fetch throw an error when the server replies 404?

**Answer:** No, it is a reply, so you must check response.ok. fetch only throws when no reply comes back, such as a network failure. A 404 or 500 is still a reply, so check response.ok before you use the data.

---

Source: https://learn.benibix.com/learn/development-web/fetch-api-and-json
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
