-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscript.js
More file actions
34 lines (28 loc) · 797 Bytes
/
script.js
File metadata and controls
34 lines (28 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//create JSON to object using AJAX
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
let student = JSON.parse(this.responseText);
console.log(student)
}
}
xhr.open('GET', 'data.json', true);
xhr.send();
//create JSON to object using Jquerry
$.getJSON('data.json', function (data) {
console.log(data);
})
////create JSON to object using fetching with promise
fetch('data.json')
.then(dataJSON => {
return dataJSON.json();
}).then(data => {
console.log(data);
});
////create JSON to object using fetching with async await
const fetchData = async url => {
const response = await fetch(url);
const data = await response.json();
console.log(data);
};
fetchData('data.json');