Skip to content

Instantly share code, notes, and snippets.

@MajhiRockzZ
Created August 29, 2019 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MajhiRockzZ/85cc574a66c90c358541c038b97a3ade to your computer and use it in GitHub Desktop.
Save MajhiRockzZ/85cc574a66c90c358541c038b97a3ade to your computer and use it in GitHub Desktop.
AJAX JSON FETCH
const DOG_URL = "https://dog.ceo/api/breeds/image/random";
// console.log(DOG_URL);
// const promise = fetch(DOG_URL);
const doggos = document.querySelector(".doggos");
// console.log(promise);
function addNewDoggo() {
const promise = fetch(DOG_URL);
promise
.then(function(response) {
const processingPromise = response.json();
// console.log(processingPromise);
return processingPromise;
})
.then(function(processedResponse) {
// console.log(processedResponse.status);
console.log(processedResponse);
const img = document.createElement("img");
img.src = processedResponse.message;
img.alt = "Cute doggo";
doggos.appendChild(img);
});
}
document.querySelector(".add-doggo").addEventListener("click", addNewDoggo);
// console.log("this will log first");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment