I'm trying to get the keys and values from a JSON array, and use them to construct a table. The JSON array has been obtained correctly with AJAX.
{"CPU":"1.23","NetworkIN":"4.56","NetworkOUT":"0.00","Uptime":"141"}
I want to turn the array above, into a table constructed like this.
<tbody id="resourceTable">
<tr>
<td>CPU</td>
<td>1.23</td>
</tr>
<tr>
<td>NetworkIN</td>
<td>4.56</td>
</tr>
<tr>
<td>NetworkOUT</td>
<td>0.00</td>
</tr>
<tr>
<td>Uptime</td>
<td>141</td>
</tr>
</tbody>
I've used the following code, which hasn't worked.
<script>
$(document).ready(function(){
var resoruce_refresh = setInterval(function(){
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{}",
url: "http://localhost/json.php",
dataType: "json",
success: function(data){
var data_keys = Object.keys(data);
for(var i = 0; i < data.length; i++){
var resource_append = "<tr><td>" + data_keys[i] + "</td><td>" + data[i] + "</td></tr>";
$("tbody#resource").append(resource_append);
}
},
error: function(result){
console.log("Failed to get JSON.");
},
});
}, 10000);
});
</script>
This script returns nothing, but the web browser reports a successful JSON retrieval. I've tried creating the table by doing:
document.getElementById("tbody#resourceTable").innerHTML = "<tr><td>............";
document.getElementById("resourceTable").innerHTML = "<tr><td>............";
Neither work.