Как это выглядит?Конечно, вам нужно было бы реализовать вызов AJAX самостоятельно и еще много чего, но вы поняли?Если вы хотите использовать xhr
, это достаточно справедливо, но fetch - это просто более новый и приятный способ реализации xhr
.
.
const url = 'https://api.data.gov.sg/v1/transport/traffic-images';
let appRoot, btn;
// Go get the data, return a promise.
const getData = () => fetch(url).then(imgs => imgs.json());
// Output the image.
const processImage = img => {
const el = document.createElement("img");
el.setAttribute("src", img.image);
appRoot.append(el);
};
// Iterate over the data from the getData function.
const process = data => {
appRoot.innerHTML = "";
data.items.forEach(o => o.cameras.forEach(img => processImage(img)));
};
// Bind the event hanlder to the event.
const bindEvents = () => btn.onclick = () => getData().then(process);
// A simple function to launch everything.
const launch = () => {
console.log('Starting...');
btn = document.getElementById("loadDoc");
appRoot = document.getElementById("demo");
bindEvents();
};
// Start the app.
launch();
<button type="button" id="loadDoc">IMAGE TEST</button>
<div id="demo"></div>