This is JavaScript program demonstrates how to fetch a file with JSON data.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>XoaX.net's Javascript</title>
<!-- Supply a transparent 1x1 favicon -->
<link rel="icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=">
<script type="text/javascript" src="FetchJson.js"></script>
<style type="text/css">
th {
background-color: lightgray;
}
td {
background-color: white;
}
</style>
</head>
<body onload="Initialize()">
<table id="idChurchFathers" border="3" cellspacing="0" cellpadding="5"></table>
</body>
</html>async function Initialize() {
// NOTE: THIS REQUIRES A WEB SERVER TO RUN
let sFileName = "ChurchFathers.json";
const kqTable = document.getElementById("idChurchFathers");
fetch(sFileName).then((qResponse) =>{
if (!qResponse.ok) {
throw new Error(`HTTP error, status = ${qResponse.status}`);
}
return qResponse.json();
}).then((sJson) =>{
// Add the row of headers
const kqHeaderRow = document.createElement("tr");
kqTable.appendChild(kqHeaderRow);
const kqNameHeader = document.createElement("th");
kqNameHeader.textContent = "Name";
kqHeaderRow.appendChild(kqNameHeader);
const kqYearOfDeathHeader = document.createElement("th");
kqYearOfDeathHeader.textContent = "Year of Death";
kqHeaderRow.appendChild(kqYearOfDeathHeader);
const kqDescriptionHeader = document.createElement("th");
kqDescriptionHeader.textContent = "Description";
kqHeaderRow.appendChild(kqDescriptionHeader);
// Add the rows of data that were read from the JSON file
for (const kqChurchFather of sJson["Church Fathers"]) {
const kqTableRow = document.createElement("tr");
const kqNameElement = document.createElement("td");
kqNameElement.textContent = kqChurchFather.Name;
kqTableRow.appendChild(kqNameElement);
const kqYearOfDeathElement = document.createElement("td");
kqYearOfDeathElement.textContent = kqChurchFather["Year of Death"];
kqTableRow.appendChild(kqYearOfDeathElement);
const kqDescriptionElement = document.createElement("td");
kqDescriptionElement.textContent = kqChurchFather["Description"];
kqTableRow.appendChild(kqDescriptionElement);
kqTable.appendChild(kqTableRow);
}
}).catch((qError) => {
const qErrorElement = document.createElement("p");
qErrorElement.appendChild(document.createTextNode(`Error: ${qError.message}`));
document.body.insertBefore(qErrorElement, kqTable);
});
}
{
"Church Fathers": [
{ "Name": "Clement of Rome", "Year of Death": "A.D. 97", "Description": "The Fourth Pope" },
{ "Name": "Ignatius", "Year of Death": "A.D. 110", "Description": "The third Bishop of Antioch" },
{ "Name": "Polycarp", "Year of Death": "A.D. 155", "Description": "Bishop of Smyrna" },
{ "Name": "Justin Martyr", "Year of Death": "A.D. 165", "Description": "Theologian, Apologist, and Martyr" },
{ "Name": "Irenaeus", "Year of Death": "A.D. 202", "Description": "Bishop of Lyons" },
{ "Name": "Cyprian", "Year of Death": "A.D. 258", "Description": "Bishop of Carthage" },
{ "Name": "Athanasius", "Year of Death": "A.D. 373", "Description": "Bishop of Alexandria" },
{ "Name": "Basil", "Year of Death": "A.D. 379", "Description": "Bishop of Caesarea" },
{ "Name": "Cyril of Jerusalem", "Year of Death": "A.D. 386", "Description": "Bishop of Jerusalem" },
{ "Name": "Ambrose", "Year of Death": "A.D. 397", "Description": "Bishop of Milan" },
{ "Name": "John Chrysostom", "Year of Death": "A.D. 407", "Description": "Archbishop of Constantinople" },
{ "Name": "Jerome", "Year of Death": "A.D. 420", "Description": "Bible Translator" },
{ "Name": "Augustine", "Year of Death": "A.D. 430", "Description": "Bishop of Hippo" },
{ "Name": "Cyril of Alexandria", "Year of Death": "A.D. 444", "Description": "Bishop of Alexandria" },
{ "Name": "Pope Leo the Great", "Year of Death": "A.D. 461", "Description": "The 45th Pope" },
{ "Name": "Pope Gregory the Great", "Year of Death": "A.D. 604", "Description": "The 64th Pope" }
]
}
© 20072026 XoaX.net LLC. All rights reserved.