JS: Fetch API
JS içinde Fetch API konusunu örneklerle öğren.
JS — Fetch API
Genel Bakış
Fetch ile API’lerden veri (JSON/metin) alırsın.
Öğreneceklerin
- Temel fetch
- JSON okuma
- Hata yönetimi
- async/await
Sözdizimi
const res = await fetch(url);
Örnek
async function load(){
try{
const res = await fetch("https://api.github.com/zen");
if(!res.ok) throw new Error("HTTP " + res.status);
const text = await res.text();
console.log(text);
}catch(err){
console.error(err);
}
}
load();
Notlar
- res.ok kontrol et.
- async/await ile try/catch kullan.
Kendin Dene: Playground aç: /tr/playground/html