Skip to content
IT glossary
FrontendUpdated December 3, 2026

Fetch API

A built-in web API that lets JavaScript make HTTP requests and read responses without reloading the page. Its main function is `fetch()`.

Also known as: AJAX, XMLHttpRequest

What the Fetch API does

When a user types into a search box, a page can ask the server for matching results and update just the suggestions. The Fetch API handles the request and response; your application decides how to display the results or what to show if the request fails. It's also used to submit forms, load another page of results, or download files.

Fetch, AJAX, and XMLHttpRequest aren't interchangeable names. AJAX describes the technique of exchanging data with a server without a full page reload. XMLHttpRequest is an older browser API that delivers results through events. Fetch is a promise-based way to do much of the same work and also supports reading response bodies as streams.

What happens when you call fetch()

Pass a URL or a Request object to fetch(), along with any options you need. The default method is GET. To send JSON with a POST, for example, set the Content-Type header to application/json and pass JSON.stringify(data) as the request body.

The promise resolves to a Response once the status and headers are available, even if the body is still arriving. Reading the body is a separate async step. Use response.json() to read and parse JSON, or response.text() to read plain text. Both consume the body, so you can't read it as text first and then call json() on the same response.

Example: cancel an outdated search

let controller;

async function search(query) {
  controller?.abort();
  controller = new AbortController();

  const params = new URLSearchParams({ q: query });
  const response = await fetch(`/api/search?${params}`, {
    signal: controller.signal,
  });

  if (!response.ok) throw new Error(`HTTP ${response.status}`);
  return response.json();
}

Each call cancels the previous request if it's still in progress. The caller should handle rejected promises and avoid showing an error for an expected AbortError. Cancellation stops the client from continuing to wait for or read the response; it doesn't guarantee that the server stopped processing the request.

Common pitfalls and interview questions

A classic interview question is: Does a 404 trigger catch? Not by itself. A 404 is a valid HTTP response, so fetch() resolves. For most API calls, check response.ok, which is true for statuses 200 through 299. Network failures, browser CORS violations, and cancellation before a response arrives can reject the fetch promise. Body parsing can fail separately: a 204 No Content response passes the status check but has nothing for json() to parse.

Another common question is whether mode: 'no-cors' fixes a cross-origin request. It doesn't grant access to the response. An opaque response hides its status, headers, and body from your script. To read a cross-origin API response in the browser, the server needs to allow it through CORS.