diff --git a/app.js b/app.js index ffe9d90..33a9c4c 100644 --- a/app.js +++ b/app.js @@ -7,23 +7,13 @@ button.addEventListener('click', function(){ }) function getRandomJoke(){ - const ajax = new XMLHttpRequest; - const url = 'https://api.chucknorris.io/jokes/random' - ajax.open('GET', url, true); - - ajax.onreadystatechange = function(){ - if(this.status === 200 && this.readyState === 4){ - console.log(this.responseText); - let data = JSON.parse(this.responseText); - jokeDIV.innerHTML = `${data.value}` - } else { - this.onerror = onerror(); - } - } - ajax.send(); + fetch('https://api.chucknorris.io/jokes/random') + .then(res => res.json()) + .then(jokeObj => { + jokeDIV.innerHTML = jokeObj.value + }) + .catch(e => { + jokeDIV.textContent = 'There was an error!' + }) } - -function onerror(){ - jokeDIV.textContent = 'There was an error!'; -} \ No newline at end of file