JSON warm up
AJAX fetch with JSON
AJAX fetch with text and JSON
CP 3 - JS with AJAX
Worksheet (You may work with your neighbor)
For reference
Coming soon! Output Comparison Tool Version 2 and Version 3.... Iteration is the key!
Talk with your neighbor
fetch
related to AJAX? Promise
? Promise
fetch
and Promise
related?We initiate a fetch
of a URL
A fetch
returns a Promise
Promises are good because...
How could we rewrite this site to use the organizational power of the JSON formatted file?
What do we have to change to the method that does the AJAX call?
function loadAssignments() {
const url = "diff.txt";
fetch(url)
.then(checkStatus)
.then(handleLoadAssignments)
.catch(console.log);
}
{
"assignments": [
{
"assignmentName": "Hello Buggy",
"numbered": false,
"disabled": false,
"expectedOutput": [
{
"outputName": "Hello Buggy",
"outputFile": "HelloBuggy-expected-output.txt"
}
]
}
...
]
}
function loadAssignments() {
const url = "diff.json";
fetch(url)
.then(checkStatus)
.then(JSON.parse)
.then(handleLoadAssignments)
.catch(console.log);
}
function handleLoadAssignments(data) {
let assignments = $("assignments");
assignments.innerHTML = ""; // clear it out
...
for (let i = 0; i < data.assignments.length; i++) {
if (data.assignments[i].disabled === false) {
let opt = document.createElement("option");
opt.value = data.assignments[i].assignmentName;
opt.innerText = data.assignments[i].assignmentName;
assignments.appendChild(opt);
}
}
assignments.disabled = false;
}
Running solution here
What if we wanted to have one text file, a "table of contents" of sorts, that listed all of the possible assignments for a quarter.
Each assignment then had it's OWN JSON file that contained the information about only that assignment.
Questions:
function loadAssignments() {
const url = "assignments.txt";
fetch(url)
.then(checkStatus)
.then(handleLoadAssignments)
.catch(console.log);
}
function handleLoadAssignments(responseData) {
let assignments = $("assignments");
data = responseData.split("\n");
assignments.innerHTML = ""; // clear it out
...
for (let i = 0; i < data.length; i++) {
loadSpecificAssignment(data[i], handleLoadOneAssignment);
}
assignments.disabled = false;
}
Running solution here
// A neat trick to reuse the "callAJAX" pattern
function loadSpecificAssignment(assignment, handleResponse) { // pass in a function
const url = assignment.replace(" ", "_") + ".json";
fetch(url)
.then(checkStatus)
.then(JSON.parse)
.then(handleResponse) // use the function here
.catch(console.log);
}
function handleLoadOneAssignment(data) {
let assignments = $("assignments");
if (data.disabled === false) {
...
opt.value = data.assignmentName;
let name = data.assignmentName;
if ("randomSeed" in data) {
name += " (random seed = " + data.randomSeed + ")";
}
opt.innerText = name;
assignments.appendChild(opt);
}
}