I have an ajax function which sends a get request to an api and retrieves some JSON, which I am displaying in a table. This is what I have tried-
<script>
function getInfo() {
$.ajax({
type: "GET",
url: "http://example.com/",
contentType: "application/json; charset=utf-8",
crossDomain: true,
dataType: "json",
success: function (data, status, jqXHR) {
for (var i=0;i<data.length;i++) {
$("table.mytable").append("<tr><td>First Name</td><td>" + data[i].firstname + "</td></tr><tr><td>Last Name</td><td>" + data[i].lastname +
"</td></tr><tr><td>Queues</td><td>" +
data[i].skills + "</td></tr>" );
}
alert("success");
},
error: function (jqXHR, status) {
// error handler
console.log(jqXHR);
alert('fail' + status.code);
}
});
}
</script>
However, the output on the html page looks like this-
First Name John Last Name Smith Skills Maths First Name Jane Last Name Smith Skills Maths
I would like the First Name, Last Name and Skills headers to be across the table, with the data underneath. Any idea how to do this?
tdis the start of a new column, so, your first row should be hardcoded to be the headers, then just append yourdatainfo.