Review AJAX with fetch
and JSON
Practice using JSON as a data format to process with JS
Build a cool Trivia webpage!
JSON is a way of saving or storing javascript objects (the technical term is "serializing" which is just a fancy word for turning an object into a savable string of characters)
Browser JSON methods:
JSON.parse( /* JSON string */ )
-- converts JSON string into Javascript object
JSON.stringify( /* Javascript Object */ )
-- converts a Javascript object into JSON text
We'll build this
Start with this index.html and starter.js (there is no CSS).
Step 1: Determine the url we want to fetch from:
CSE 154 Trivia web service url:
Step 2: Determine what questions we can ask:
mode=all
(required)The Trivia Web Service's HTTP response will look like this:
{
"trivia": [
{
"question": "What's a cat?",
"answer": "An animal!",
"difficulty": 3
},
{
"question": "What's a dog?",
"answer": "An even better animal!",
"difficulty": 1
},
...
]
}
Knowing the URL and parameters, how can we use fetch
to get
the JSON response containing trivia?
https://courses.cs.washington.edu/courses/cse154/webservices/trivia/trivia.php?mode=all
Fetch the JSON and log it in your response function, then set a breakpoint in your browser tools to see if you have the correct parameter format in the function.
Also check the Network tab to see the response!
Fill the #q-output
paragraph with a random question from the JSON result when "New Question" is pressed.
Note that the buttons on the page are initially disabled. You should enable them when the JSON data from Exercise 1 is successfully stored.
If there is an error when fetching the data, "catch it" and populate
#q-output
with a user-friendly error message of your choice.
(tips on getting random array value on slide below)
Choose a random element from a list like so:
/**
* Returns an element chosen randomly from a given array.
* @param {[*]} choices - array of elements
* @returns {*} - a random element of choices
*/
function randomChoice(choices) {
let index = Math.floor(Math.random() * choices.length);
return choices[index];
}
Now, let’s fill in the corresponding answer when "Show Answer" is pressed.
You may want to think about storing the just-shown question in a module-global variable. Question: Why is this an appropriate choice for a module-global variable?
After the "Show Answer" button is pressed, it should be disabled until a new question is chosen (in which case it should be re-enabled). See the slide below for an example output.
Before clicking Show Answer
After clicking Show Answer
What's trivia without some difficulty to measure against? In this part, we'll use the difficulty provided in the JSON data to add more information for each question.
Note that in the JSON data, there is a difficulty
attribute for each
item: a difficulty value of 1
corresponds to Easy, 2
to Medium, and
3
to Hard.
Example output
Let's try out our first public API!
There is a trivia API you may use at opentdb.com.
Update your solution from the earlier slides to use this API instead of the one we gave you, and replace
the "trivia"
JSON attribute with "results"
and "answer"
to "correct_answer"
.
When looking over the response JSON, it will be pretty messy since there is so much information and the API doesn't format the JSON output as we do. We recommend using this JSON Viewer Chrome Extension for easily viewing the JSON structure in your browser!
You may alternatively find it useful to copy/paste the JSON with this JSON "pretty print" tool and compare with the JSON output you used in Part I.