The-Async-Force-Ep-2
An async Exercise using client side XHR accessing the swapi API at https://swapi.co
Excercise
Remember to commit after each step.
-
Make a
/js
folder and create anapp.js
file in it. -
In the
index.html
file, loadapp.js
via script tags. -
Use live-server to serve up the files to your browser.
-
Construct your XHR requests in the
app.js
file by scraping the value from the input field. -
A request to the SWAPI api should be initiated when the button is clicked. Information that comes back from the API should be displayed in the body of the html. Additional html elements should be created to properly parse, display and format the data in a presentable way.
-
If a
person
is requested, display the following information: - Name, in an<h2>
tag - Gender, in an<p>
tag - Species (only get the first species if there are multiple) (only display the name), in an<p>
tag -
If a
planet
is requested, display the following information: - Name, in an<h2>
tag - Terrain, in an<p>
tag - Population, in an<p>
tag - a list of all Film names that this planet appeared in<li>
tags wrapped in<ul>
-
If a
starship
is requested, display the following information: - Name, in an<h2>
tag - Manufacturer, in an<p>
tag - Starship Class, in an<p>
tag - a list of all Film names that this starship appeared in<li>
tags wrapped in<ul>
-
Display any xhr errors in the dom, so the user can see. For example, if a user inputs
99999
as an id, orpotato
. Display the error on the page.
Note: Go to https://swapi.co to preview the data that you will be requesting
Demo
Hints
- URL Fragments are the parts of a URL that an API Endpoint parses to tell the server what resource to retrieve.
- Slides on Async HTTP Request with XHR: http://slides.com/sgnl/xhr#/
- Documentation on 'XMLHttpRequest' method: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest
- Remember that this is a client side excercise which means that all the code will be executed via the browser's javascript runtime, which means that our global context is the 'window' object. Go ahead and type in 'window' in the browser console to inspect the methods on the 'window' object. In there you will find a method named 'XMLHttpRequest' which you will be using for this excercise.