JavaScript

Checking response.ok

Në shqip: Kontrolli i përgjigjes së fetch

ShpjegimiSQ

fetch() s'hedh gabim kur serveri kthen 404 ose 500 — vetëm kur s'ka fare lidhje. Prandaj kontrollo gjithmonë response.ok (true për statuset 200–299) para se ta lexosh përgjigjen.

EnglishEN

fetch() doesn't throw when the server returns 404 or 500 — only when there's no connection at all. So always check response.ok (true for statuses 200–299) before reading the response.

Si ta mendosh

Si ta hapësh pakon para se ta pranosh — mos e pranon vetëm se arriti.

Lexoje në anglisht

Like opening the parcel before signing for it — don't accept it just because it arrived.

Shembull kodijavascript

const r = await fetch("/api/terma");
if (!r.ok) throw new Error("Gabim " + r.status);
const termat = await r.json();

Terma të lidhur