Skip to content

Commit

Permalink
added example of working with json in ajax
Browse files Browse the repository at this point in the history
  • Loading branch information
chadsmith12 committed May 13, 2018
1 parent 0515be4 commit 8dabe5d
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
55 changes: 55 additions & 0 deletions ajax-json/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Example of using ajax to load up a JSON file.
* Examples shows working with json files
*/

document.getElementById("getSingleProductBtn").addEventListener('click', loadProduct);
document.getElementById("getProductsBtn").addEventListener('click', loadProducts);

// load a single product
function loadProduct(e) {
const xhr = new XMLHttpRequest();
xhr.open('GET', "testFiles/product.json", true);

xhr.onload = function () {
if(this.status === 200) {
const product = JSON.parse(this.responseText);
const output =
`<ul>
<li>Id: ${product.id}</li>
<li>SKU: ${product.sku}</li>
<li>Product Name: ${product.productName}</li>
</ul>
`
document.querySelector("#product").innerHTML = output;
}
}

xhr.send();
}

// load an array of products
function loadProducts(e) {
const xhr = new XMLHttpRequest();
xhr.open('GET', "testFiles/products.json", true);

xhr.onload = function () {
if(this.status === 200) {
const products = JSON.parse(this.responseText);
let output = '';
products.forEach(product => {
output +=
`<ul>
<li>Id: ${product.id}</li>
<li>SKU: ${product.sku}</li>
<li>Product Name: ${product.productName}</li>
</ul>
`
});

document.querySelector("#products").innerHTML = output;
}
}

xhr.send();
}
23 changes: 23 additions & 0 deletions ajax-json/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" />
<title>XHR Objects - Working With Text</title>
</head>
<body>
<div class="container">
<button id="getSingleProductBtn">Get Product</button>
<button id="getProductsBtn">Get Products</button>
<br><br>
<h3>Product</h3>
<div id="product"></div>
<h3>Products</h3>
<div id="products"></div>
</div>

<script src="app.js"></script>
</body>
</html>
5 changes: 5 additions & 0 deletions ajax-json/testFiles/product.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": 1,
"sku": "11234",
"productName": "Computer Monitor"
}
17 changes: 17 additions & 0 deletions ajax-json/testFiles/products.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"id": 1,
"sku": "11234",
"productName": "Computer Monitor"
},
{
"id": 2,
"sku": "12345",
"productName": "Computer Hardrive"
},
{
"id": 3,
"sku": "17415",
"productName": "Computer Processor"
}
]

0 comments on commit 8dabe5d

Please sign in to comment.