Design comparison
SolutionDesign
Solution retrospective
For some reason I can't manage to make the code work on the button click but on page refresh it seems to work. Does anyone have an idea of what I'm missing there ? first time working with API and promises to, it was quite complicate to understand how to make things work
Community feedback
- @zougari47Posted over 2 years ago
Hi there👋,
I have faced this problem before, you need to clear cache every time you use fetch API
This is the solution for your code
function getAdvice(){ fetch('https://api.adviceslip.com/advice', { cache: 'no-cache' }).then(response =>{ return response.json(); }).then(adviceData =>{ const adviceObj = adviceData.slip; resDiv.innerHTML = `<p>${adviceObj.advice}</p>`; resTitle.innerHTML = `ADVICE #${adviceObj.id}`; console.log(adviceObj.advice); }).catch(error =>{ console.log(error); }) } ``
Marked as helpful0@MarcusTuliusCiceronPosted over 2 years ago@zougari47 :D yeah that works, thank you very much for info
0
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord