I am going to fetch a json file from jsonbin when clicking a button, but it is not working, your help is appreciated.
<button id="runxhr">RUN XHR</button>
<div id="main"></div>
js file is here:
function runXHR() {
let xhr = new XMLHttpRequest();
addListeners(xhr);
xhr.open(
"GET",
"https://api.jsonbin.io/v3/b/62b4395c449a1f3821167058/",
true
);
xhr.setRequestHeader("X-Bin-Meta", "false");
xhr.send();
return xhr.responseText;
}
function addListeners(xhr) {
xhr.addEventListener("loadend", display(xhr));
}
function display(jsonstring) {
let main = document.getElementById("main");
json = JSON.parse(jsonstring);
main.innerHTML = json[0]["name"];
}
const buttnRun = document.getElementById("runxhr");
buttnRun.addEventListener("click", () => {
runXHR();
});