let btn = document.getElementById("call_fetch_btn");
btn.addEventListener("click", wakeUpFetch);
function wakeUpFetch() {
// Try it with other ressources to evoke an error:
fetch("resources/resource.php")
.then((response) => {
// Check the HTTP-Status of the response
if (!response.ok) {
throw new Error('HTTP error, status = ' + response.status + ' - Ressource hat nicht geantwortet');
}
else {
// Check the Content-Type in the header of the response
const ContentType = response.headers.get('content-type');
const isContentTypeCorrect = ContentType.includes('application/json');
if (!isContentTypeCorrect) {
throw new Error('Der Content-Type ist: ' + ContentType + ' - Leider kein JSON');
}
else {
return response.json();
}
}
})
.then((data) => console.log("Resultat: ", data.a))
// Error-Spaghetti
.catch((error) => {
console.log('Es gab einen Fehler!', error)
});
}