From 83ea0e880831a5b7c7f27c239d101e1e8f5eb17c Mon Sep 17 00:00:00 2001 From: hairyElephant Date: Wed, 7 Dec 2022 20:23:07 -0500 Subject: [PATCH 1/3] didn't work --- lib/script.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/script.js b/lib/script.js index e69de29..93f78ff 100644 --- a/lib/script.js +++ b/lib/script.js @@ -0,0 +1,19 @@ +const form = document.querySelector('form') +const url = 'https://api.thecatapi.com/v1/images/search' +const randomButton = document.querySelector('.randomButton') + +let header = { + "x-api-key": 'live_kP5CcKWDcyIUvIeZz7c8NgRuPT6HZT2pxDcRbgNvzSbKASbAxaNLVZPflqQXqvfk' +} +function handleClick(){ + fetch(url, { header }) + .then((res) => res.json()) + .then((res) => { + let ranImage = document.querySelector('randomCatImage'); + ranImage.src = res[0].url + }) +} + +randomButton.addEventListener("click", handleClick) + + From 09bed1dde75e2c05dda1e7caa9ec2b8a65452fc9 Mon Sep 17 00:00:00 2001 From: hairyElephant Date: Sat, 10 Dec 2022 19:48:58 -0500 Subject: [PATCH 2/3] Finally got it to work --- lib/script.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/script.js b/lib/script.js index 93f78ff..94a67da 100644 --- a/lib/script.js +++ b/lib/script.js @@ -1,19 +1,21 @@ -const form = document.querySelector('form') -const url = 'https://api.thecatapi.com/v1/images/search' -const randomButton = document.querySelector('.randomButton') +const apiKey = 'live_kP5CcKWDcyIUvIeZz7c8NgRuPT6HZT2pxDcRbgNvzSbKASbAxaNLVZPflqQXqvfk' let header = { "x-api-key": 'live_kP5CcKWDcyIUvIeZz7c8NgRuPT6HZT2pxDcRbgNvzSbKASbAxaNLVZPflqQXqvfk' } -function handleClick(){ - fetch(url, { header }) - .then((res) => res.json()) - .then((res) => { - let ranImage = document.querySelector('randomCatImage'); - ranImage.src = res[0].url - }) -} -randomButton.addEventListener("click", handleClick) +const url = `https://api.thecatapi.com/v1/images/search` + +const randomButton = document.querySelector('.randomButton') + +const kittyIMG = document.querySelector('.categoryCatImage') + + +randomButton.addEventListener('click', function(){ + + fetch(url, {header}) + .then((response) => response.json()) +.then((response) => { kittyIMG.src = `${response[0].url}`}) +}) From 7e3c53f31c9bdedb82346105b7e0eb3429a49a0f Mon Sep 17 00:00:00 2001 From: hairyElephant Date: Sat, 10 Dec 2022 20:07:49 -0500 Subject: [PATCH 3/3] Just trying different things --- lib/script.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/script.js b/lib/script.js index 94a67da..1457992 100644 --- a/lib/script.js +++ b/lib/script.js @@ -1,10 +1,6 @@ const apiKey = 'live_kP5CcKWDcyIUvIeZz7c8NgRuPT6HZT2pxDcRbgNvzSbKASbAxaNLVZPflqQXqvfk' -let header = { - "x-api-key": 'live_kP5CcKWDcyIUvIeZz7c8NgRuPT6HZT2pxDcRbgNvzSbKASbAxaNLVZPflqQXqvfk' -} - -const url = `https://api.thecatapi.com/v1/images/search` +const url = `https://api.thecatapi.com/v1/images/search/?api_key=${apiKey}` const randomButton = document.querySelector('.randomButton') @@ -13,7 +9,7 @@ const kittyIMG = document.querySelector('.categoryCatImage') randomButton.addEventListener('click', function(){ - fetch(url, {header}) + fetch(url) .then((response) => response.json()) .then((response) => { kittyIMG.src = `${response[0].url}`})