Giter Club home page Giter Club logo

pursuit-core-web-ajax-lab's Introduction

Pursuit-Core-Web-AJAX-Lab

Alright! It's time to employ our friend the Dog API again. However, we aren't going to grab data manually from it anymore - we're going to use XMLHttpRequest to get it each time the user asks.

Also, we aren't just going to grab random dog breeds. We're going to grab random dog images and present them to the user! What a therapeutic little app.

Step 1

Create an HTML file and a JavaScript file. In your HTML file, create a button element and import your JavaScript.

In your JavaScript, add an event listener the button. For now, let's just have the listener in our button log "Clicked!" to the console.

Now, let's make a function called getRandomImage. This function should use XMLHttpRequest to make an AJAX GET request to the random image route https://dog.ceo/api/breeds/image/random of the Dog API. Get the response, parse it so that it becomes a JavaScript object and log the object to the console.

Now, change your button event listener to call getRandomImage. Notice that this object has two keys: a status key (success! yay!) and a message key, which contains an image URL.

Step 2

So, our button now triggers an AJAX request. However, it doesn't access our DOM to do anything - yet. Let's add more functionality to our event listener. Assign the result of our XMLHttpRequest to a variable. Then, create a DOM node representing an img element.

Keying into the DOM node at the key of src, we can add a src to our img tag. Assign the src of our DOM node to the image URL we get from our AJAX request. Then, append our img to the end of our HTML body.

Click the button. You should see a random dog image appear. Yay!

Step 3

Now that we've got one random dog image, we may want another. However, we don't want to append another image to the bottom of our page - we want to replace the existing image with a new one. Add a condition to your event listener to replace the image if it already exists (hint: while it isn't the only way to do it, the replaceChild method may prove useful here).

Awesome. We've built out the core functionality of our Random Dog Image app. Style it to your liking.

Bonus

  • Add a select element. Allow the user to select a breed and get a random image of a dog from that breed specifically. Here's an example query to the Dog API to get a random image for a dog with the breed hound: https://dog.ceo/api/breed/hound/images/random.
  • Save all of the previous images instead of overwriting them. Use a Flexbox to arrange them on the page.

pursuit-core-web-ajax-lab's People

Contributors

alejo4373 avatar benstone1 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.