Reputation: 23
I have the below ajax
calling a json
file to display in an HTML table. Initially it worked but is now returning UNDEFINED
.
Is it possible that the json
file hasn't finished loading before the script continues on?
$(document).ready(function() {
var displayUsers = $('#stage');
displayUsers.text('Retreiving users...');
$.ajax({
type: "GET",
dataType: "json",
url: "users.json",
success: function(result) {
console.log(result);
var output = "<table id='user-table' class='table table-hover'><thead><tr><th onclick='sortTable(0)' class='main-header' data-is-only-head='false' style='text-align: left;'>ID</th><th onclick='sortTable(1)' class='main-header' data-is-only-head='false' style='text-align: left;'>Name</th><th onclick='sortTable(2)' class='main-header' data-is-only-head='false' style='text-align: left;'>HighFives</th><th onclick='sortTable(3)' class='main-header' data-is-only-head='false' style='text-align: left;'>Excluded</th></thead><tbody>";
for (var i in result) {
output += "<tr><td>" + result[i].id + "</td><td>" + result[i].name + "</td><td>" + result[i].reward + "</td><td>" + result[i].excluded + "</td></tr>";
}
output += "</tbody></table>";
displayUsers.html(output);
$("table").addClass("table");
}
});
});
Sample of json file:
{"id":"U123456","gift":0,"reward":0,"name":"joe.smith","excluded":true}
Upvotes: 1
Views: 71
Reputation: 50728
The sample you provided doesn't match the code; your code should look like the following based on the snippet you provided:
output += "<tr><td>" + result.id +
"</td><td>" + result.name +
"</td><td>" + result.reward +
"</td><td>" + result.excluded + "</td></tr>";
Upvotes: 1