The Fetch API in JavaScript
The Fetch API is the modern, built-in way to make network requests in JavaScript. It replaces older methods like XMLHttpRequest and works perfectly with Promises and async/await.
Fetch is ideal for loading data from servers, sending forms, uploading files, or communicating with REST APIs. It's supported in all modern browsers and is simple yet powerful.
We'll cover everything step by step: basic requests, different HTTP methods, headers, JSON handling, response processing, error handling, and even advanced features like aborting requests.
Basic GET Request – Fetching Data
The simplest use is a GET request to retrieve data. Fetch returns a Promise that resolves to a Response object.
Always check response.ok and handle non-2xx statuses properly.
Using async/await – Cleaner Syntax
async/await makes Promise code read like synchronous code – much easier to follow.
POST Request – Sending Data
Use method: "POST" and include a body. For JSON, set the Content-Type header.
PUT and DELETE Requests
Update (PUT/PATCH) or delete (DELETE) resources.
Headers and Custom Options
Headers control content type, authentication, caching, etc.
Handling Non-OK Responses
Fetch doesn't reject on HTTP errors (4xx/5xx) – check response.ok manually.
Aborting Requests with AbortController
Cancel long-running requests (e.g., user navigates away).
Quick Quiz
Wir haben die Materialien überprüft, dennoch können Fehler vorkommen. Der Inhalt dient ausschließlich Bildungszwecken, daher verwende ihn auf eigene Verantwortung und überprüfe ihn bei Bedarf mit anderen Quellen.
✨ Frag Lara — deine KI-Lernpartnerin
Entsperre personalisierte Lernunterstützung. Lara kann Lektionen erklären, Themen zusammenfassen und deine Lernfragen beantworten — verfügbar ab dem Go-Tarif.
Lara hilft dir, schneller zu lernen — exklusiv für ReadyTools Go-, Plus- und Max-Mitglieder.
