Check response.ok
fetch does not fail on 404 or 500. Always check response.ok yourself.
AJAX · Check response.okBeginner
async function getJSON(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Request failed with status ' + response.status);
}
return response.json();
}
getJSON('/api/items').then(console.log).catch(function (err) { console.error(err.message); });fetcherrorsstatusok