Created by
Abram Hindle
(abram.hindle@ualberta.ca)
and Hazel Campbell (hazel.campbell@ualberta.ca).
Copyright 2014-2023.
Use JS to make an HTTP request and get the content
Mean Girls. Paramount Home Entertainment, 2005. Performed by Rachel McAdams.
<blockquote>
<img height="400" class="centered" id="fetchexample"
alt="Stop trying to make fetch happen!">
<cite><em>Mean Girls.</em> Paramount Home Entertainment, 2005. Performed by Rachel McAdams.</cite>
<button type="button" onclick="fetchExample()">Fetch!</button>
function fetchExample() {
// https://developer.mozilla.org/en-US/docs/Web/API/Request/Request#Example 2019-02-14
var img = document.querySelector("img#fetchexample");
var request = new Request("../images/fetch.gif");
var promise1 = fetch(request);
var promise2 = promise1.then(function(response) {
console.log("Got headers!");
return response.blob(); // return a promise for raw binary data
});
promise2.then(function(blob) {
console.log("Got data blob!");
var objectURL = URL.createObjectURL(blob);
img.src = objectURL;
});
}
// Note in the DOM the img's src= attribute after running!</pre></code>
function makeAPromise(data) {
return new Promise((resolve, reject) => {
resolve(data);
});
}
function promiseExample() {
// .then returns a NEW promise
var promise1 = makeAPromise("X");
var promise2 = promise1.then((data2) => {
// inner function gets called after the promise is resolved
alert("data2: " + data2);
return "Y";
});
// .then calls its argument with the return value of the
// previous promises's callback function
var promise3 = promise2.then((data3) => {
alert("data3: " + data3);
});
}
function promiseTimerExample() {
console.log("1. First line of promiseTimerExample");
const promise1 = new Promise((resolve, reject) => {
console.log("2. In new Promise #1 executor function")
window.setTimeout(() => {
console.log("4. In window.setTimeout callback function")
resolve(333);
}, 1000);
})
const promise2 = promise1.then((value) => {
console.log("5. In promise1.then "onfulfilled" function", value);
return 777;
});
const promise3 = promise2.then((value) => {
console.log("6. In promise2.then "onfulfilled" function", value);
})
console.log("3. LAST line of promiseTimerExample");
}
then
Can Return Promisefunction promise2TimerExample() {
console.log("1. First line of promiseTimerExample");
const promise1 = new Promise((resolve, reject) => {
console.log("2. In new Promise #1 executor function")
window.setTimeout(() => {
console.log("4. In window.setTimeout callback function")
resolve(333);
}, 1000);
})
const promise2 = promise1.then((value) => {
console.log("5. In promise1.then function", value);
return new Promise((resolve, reject) => {
window.setTimeout(() => {
console.log("6. Second timer timed out");
resolve(777);
}, 2000);
});
});
const promise3 = promise2.then((value) => {
console.log("7. In promise2.then function", value);
})
console.log("3. LAST line of promiseTimerExample");
}
var request = new Request("images/fetch.gif");
var promise1 = fetch(request);
var promise2 = promise1.then(function(response) {
console.log("Got headers!");
return response.blob(); // return a promise for raw binary data
});
promise2.then(function(blob) {
console.log("Got data blob!");
var objectURL = URL.createObjectURL(blob);
img.src = objectURL;
});
}
is the same as
var request = new Request("images/fetch.gif");
fetch(request).then(function(response) {
console.log("Got headers!");
return response.blob(); // return a promise for raw binary data
}).then(function(blob) {
console.log("Got data blob!");
var objectURL = URL.createObjectURL(blob);
img.src = objectURL;
});
Let's make a generic JSON GET function
function fetchJSON(url) {
var request = new Request(url);
return fetch(request).then((response) => {
if (response.status === 200) { // OK
return response.json(); // return a Promise
} else {
alert("Something went wrong: " + response.status);
}
});
}
var getterID; // global
function startGetting() {
getterID = window.setInterval(() => { // callback
var now = new Date();
var s = 1 + (now.getSeconds() % 4); // remainder
var url = s + ".json" // 1.json 2.json 3.json...
fetchJSON(url).then((json) => { // another callback
console.log(json); // browser turned the JSON into an object
text = json.message; // it has properties
document.querySelector("#ajaxy").innerText = text;
});
}, 1000); // 1 second or 1000ms
}
function stopGetting() {
window.clearInterval(getterID);
}
<button type="button" onclick="startGetting()">Start Getting</button>
<button type="button" onclick="stopGetting()">Stop Getting</button>
<blockquote id="ajaxy"></blockquote>
function stringifyExample() {
var obj = { "food":"hotdog",
"condiments":["ketchup","mustard","cheese"],
"sausage":"weiner"
};
document.querySelector("#hotdog").value =
JSON.stringify(obj, null, " "); // pretty print
}
function parseExample() {
text = document.querySelector("#hotdog").value;
var newObj = JSON.parse(text);
document.querySelector("#sausage").innerText = newObj.sausage;
}
Design Suggestions:
What are your events?
Solutions?
var getterID2; // global
async function get2() {
var now = new Date();
var s = 1 + (now.getSeconds() % 4); // remainder
var url = "../" + s + ".json" // 1.json 2.json 3.json...
var json = await fetchJSON(url);
console.log(json); // browser turned the JSON into an object
var text = json.message; // it has properties
document.querySelector("#ajaxy2").innerText = text;
}
function startGetting2() {
getterID2 = window.setInterval(get2, 1000); // 1 second or 1000ms
}
function stopGetting2() {
window.clearInterval(getterID2);
}
<button type="button" onclick="startGetting2()">Start Getting</button>
<button type="button" onclick="stopGetting2()">Stop Getting</button>
<blockquote id="ajaxy2"></blockquote>
Copyright 2014-2023 ⓒ Abram Hindle
Copyright 2019-2023 ⓒ Hazel Victoria Campbell and contributors
Other images used under fair use and copyright their copyright holders.
Copyright (C) 2019-2023 Hazel Victoria Campbell
Copyright (C) 2014-2023 Abram Hindle and contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN.
01234567890123456789012345678901234567890123456789012345678901234567890123456789