This repository has been archived by the owner on Jun 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
fetchdata.js
126 lines (113 loc) · 3.35 KB
/
fetchdata.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// window.onload = getJSON;
function setQuery() {
// to process the url to be sent to the server
const site = 'http://gymk-back.herokuapp.com' + events;
let temp = site.split('/');
let final = 'http://gymk-back.herokuapp.com/' + temp[temp.length - 1];
return final;
}
async function getJSON() {
let policy = {
mode: 'no-cors'
};
fetch('http://gymk-back.herokuapp.com/events', policy)
.then(function(response) {
// url in place of link later
console.log(response);
})
.then(function(data) {
console.log(data); // constructTable(data);
})
.catch(function(error) {
console.log(error);
});
}
getJSON();
function constructTable() {
var data = JSON.stringify('json file here'); //JSON.stringify('json file here');
/* sample format of JSON file [
{
"Book ID": "1",
"Book Name": "Computer Architecture",
"Category": "Computers",
"Price": "125.60"
},
{
"Book ID": "2",
"Book Name": "Asp.Net 4 Blue Book",
"Category": "Programming",
"Price": "56.00"
},
{
"Book ID": "3",
"Book Name": "Popular Science",
"Category": "Science",
"Price": "210.40"
}
] */ var heads = [];
for (let i = 0; i < data.length; i++) {
// get headings
for (var key in data[i]) {
if (heads.indexOf(key) === -1) {
heads.push(key);
}
}
}
var table = document.getElementById('event'); // change id accordingly
var tr = table.insertRow(-1);
for (let i = 0; i < heads.length; i++) {
var th = document.createElement('th');
th.innerHTML = heads[i];
tr.appendChild(th);
}
for (let i = 0; i < data.length; i++) {
tr = table.insertRow(-1);
for (let j = 0; j < heads.length; j++) {
var tabcell = tr.insertCell(-1);
tabcell.innerHTML = data[i][heads[j]];
}
}
}
/*
function getJSON() {
let policy = {
mode: 'no-cors'
}
const response = await fetch('http://gymk-back.herokuapp.com/events', policy);
const myJson = await response.json();
fetch('http://gymk-back.herokuapp.com/events', policy).then(function (response) {
return response.json();
}).then(function (data) {
console.log(data); // constructTable(data);
}).catch(function (error) {
console.log(error);
});
}
function getQuery(url) {
const response = await fetch(url);
const result = await response.json();
// console.log(JSON.stringify(myJson));
return result;
}
var getJSON = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status === 200) {
callback(null, xhr.response);
} else {
callback(status, xhr.response);
}
};
xhr.send();
};
getJSON(setQuery(),
function(err, data) {
if (err !== null) {
alert('Something went wrong: ' + err);
} else {
alert('Your query count: ' + data.query.count);
}
}); */