1. GET and display JSON

Load public JSON placeholder.

Output
Click load

Fetches one post from JSONPlaceholder.

const res = await fetch(url);
if (!res.ok) throw new Error(res.status);
const data = await res.json();

2. POST JSON body

Send Content-Type application/json.

Snippet
POST with JSON body\n{ "title": "Hi" }

Server must accept CORS for browser demos.

await fetch(url, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ title: 'Hi' })
});

3. AbortController

Cancel slow requests on route change.

Note

const c = new AbortController(); fetch(url, { signal: c.signal }); c.abort();

Prevents race updates.

const controller = new AbortController();
fetch(url, { signal: controller.signal });

Frequently asked questions

fetch vs XMLHttpRequest?
fetch is Promise-based and cleaner; XHR is legacy.
CORS errors?
Browser blocks cross-origin without Access-Control-Allow-Origin header. Fix on server or proxy.
Await fetch in loop?
Use Promise.all for parallel requests; avoid await in tight sequential loops when parallel is OK.