ajaxTempalte
by Ford Heacock
HTML
<button id="btnClick">
click
</button>
<div id="response">
</div>
JavaScript
const button = document.querySelector("#btnClick");
const url = "https://httpbin.org/get";
const output = document.querySelector("#response");
console.log(output);
function ajaxTemplate(trigger, url, verb, output) {
$(trigger).click(function(){
fetch(url, {
method: verb
})
.catch(() => {
console.log("Re-evaluate life. Something has gone terribly wrong.");
})
.then((res) => {
if (res.ok) {
res.json().then((json) => {
for (var key in json) {
if (json.hasOwnProperty(key)) {
console.log(key + " -> " + json[key]);
}
}
});
} else {
console.log("error", res);
}
});
});
}
ajaxTemplate(button, url, "GET", output);