I have a json data url pulling from an api. I'm trying to pull that data, using only javascript, into a table into HTML. I've tried multiple things, but no matter what, the table isn't poulating with the data from the JSON file. I was wondering if anything stood out to anyone that is clearly not working? I've just made up the url for this purpose (the real url is pulling the json file).
When I run it through developer tools, it gives me the following error:
testingjson.html:7 Uncaught ReferenceError: $ is not defined
<html>
<head>
<script>
$(function() {
var people = [];
$.getJSON('https://api.url.come', function(data) {
$.each(data.person, function(i, f) {
var tblRow = "<tr>" + "<td>" + f.organization + "</td>" +
"<td>" + f.service_url + "</td>" + "<td>" + f.account + "</td>" + "<td>" + f.created_at + "</td>" + "</tr>"
$(tblRow).appendTo("#userdata tbody");
});
});
});
</script>
</head>
<body>
<div class="wrapper">
<div class="profile">
<table id= "userdata" border="2">
<thead>
<th>Organization</th>
<th>URL</th>
<th>Account</th>
<th>Created</th>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</body>
</html>
After reading and trying all of the suggestions: this one seems the closest, but isn't pulling any data yet:
<html>
<head>
</head>
<body>
<div class="wrapper">
<div class="profile">
<table id= "userdata" border="2">
<thead>
<th>Organization</th>
<th>URL</th>
<th>Account</th>
<th>Created</th>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<script>
(function() {
var table = document.getElementById("userdata");
var people = [];
fetch('https://api.gsa.gov/systems/digital-registry/v1/social_media.json?agencies=2200')
.then(data => data.json())
.then(json => json.map(f=>{
var tblRow = "<tr>" + "<td>" + f.account + "</td>" +
"<td>" + f.service_url + "</td>" + "<td>" + f.created_at + "</td>" + "<td>" + f.id + "</td>" + "</tr>"
table.innerHTML += tblRow;
}))
})();
</script>
</body>
</html>
Now, I tried this and it still doesnt work:
<html>
<head>
<script type="text/javascript">
var url = "https://api.gsa.gov/systems/digital-registry/v1/social_media.json?agencies=2200";
var tableBody = document.getElementById("userdataBody");
var req = new XMLHttpRequest();
req.open("GET", url, true);
req.onload = function(data) {
if (req.status === 200) {
console.log("Success");
var persons = JSON.parse(req.response);
var newContent = "";
persons.forEach(function(person) {
var tblRow = "<tr>" + "<td>" + person.organization + "</td>" +
"<td>" + person.account + "</td>" + "<td>" + person.service_url + "</td></tr>";
newContent += tblRow;
});
tableBody.innerHTML += newContent;
} else {
console.log("Error : %d (%s)", req.status, req.statusText);
}
}
req.send();
</script>
</head>
<body>
<div class="wrapper">
<div class="profile">
<table id="userdata" border="2">
<thead>
<th>Name</th>
<th>Unsername</th>
<th>Email</th>
</thead>
<tbody id="userdataBody">
</tbody>
</table>
</div>
</div>
</body>
</html>