Coggle requires JavaScript to display documents.
XMLHttpRequest
const req = new XMLHttpRequest(); // success callback req.onload = function () { console.log("IT LOADED!!"); const data = JSON.parse(this.responseText); console.log(data.name, data.height); }; // failure callback req.onerror = function () { console.log("ERROR!!!!"); console.log(this); }; req.open("GET", "https://swapi.dev/api/people/1/"); req.send();
> IT LOADED!! Luke Skywalker 172
fetch
fetch("https://swapi.dev/api/people/1/") .then((res) => { console.log("RESOLVED!", res); return res.json(); // return a Promise }) .then((data) => { console.log(data.name, data.height); }) .catch((e) => { console.log("ERROR!", e); });
> RESOLVED! Response { body: ReadableStream type: 'cors', url: 'https://swapi.dev/api/people/1/', …} Luke Skywalker 172
const loadStarWarsPerson = async () => { try { const res = await fetch("https://swapi.dev/api/people/1/"); const data = await res.json(); console.log(data.name, data.height); } catch (e) { console.log("ERROR!!!", e); } }; loadStarWarsPerson();
Promise
body
ReadableStream