-
Notifications
You must be signed in to change notification settings - Fork 0
dog.ceo API
Hiren Gajjar edited this page Feb 22, 2024
·
1 revision
<button>Click</button>
<script src="https://cdn.jsdelivr.net/npm/axios@1.6.7/dist/axios.min.js"></script>
// Using fetch()
document.querySelector('button').addEventListener('click', () => {
getImgUsingFetch();
})
// Using axios()
document.querySelector('button').addEventListener('click', () => {
getImgUsingAxios();
})
// Using fetch()
async function getImgUsingFetch() {
try {
await fetch('https://dog.ceo/api/breeds/image/random').then((data) => {
return data.json();
}).then((data) => {
document.querySelector('.img').setAttribute("src", data.message);
})
} catch (err) {
console.log(err);
}
}
async function getImgUsingAxios() {
try {
await axios('https://dog.ceo/api/breeds/image/random').then((data) => {
document.querySelector('.img').setAttribute("src", data.data.message);
})
} catch (err) {
console.log(err);
}
}