@LucaJahnen
Posted
Hello Agyemang99,
You can make API Calls in JavaScript using the fetch API like this:
const makeRequest = async () => {
try {
const url = "https://api.adviceslip.com/advice"
const response = await fetch(url)
const data = response.json()
return data
} catch (error) {
// error handling
console.log(error)
}
}
// use the function to display the quote
const displayQuote = async () => {
const advice = await makeRequest()
document.querySelector("h1").textContent = advice.slip.advice
document.querySelector("p").textContent = advice.slip.id
}
displayQuote()
I am using async/await syntax. If you would like to know more about how it works I recommend this video.
Please mark this feedback as helpful if it was and let me know if you have further questions.
Happy Coding
Luca