I have a restful web service at http:// localhost:8080/CRUDWebAppMavenized/persons
that outputs a json array. I am using AJAX in jquery and i want to display the json array in my HTML table. I cannot get this to display in html table though. I will post html, javascript code, and json array code.
index3.html The HTML file. For displaying student information in the browser.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="engine.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<table id="blogTable">
<tr>
<th>studentId</th>
<th>firstname</th>
<th>lastname</th>
<th>yearLevel</th>
</tr>
</table>
</body>
</html>
engine.js (with Jquery) AJAX call and then place code in table tag of html file.
$(document).ready(function () {
$.ajax({
url: 'http://localhost:8080/CRUDWebAppMavenized/persons',
type: 'GET',
dataType: 'json',
success: function (result) {
var insert = '';
$.each(result, function (index, item) {
insert += '<tr><td>' + item.studentId + '</td><td>' + item.firstname + '</td><td>' + item.lastname + '</td><td>' + item.yearLevel + '</td></tr>';
});
$('#blogTable tr:last').after(insert);
}
});
});
JSON array A JSON array from the web service URL. This was generated by a web application.
[
{
"studentId": 5,
"firstname": "bro",
"lastname": "oy",
"yearLevel": 0
},
{
"studentId": 6,
"firstname": "lol",
"lastname": "keke",
"yearLevel": 0
},
{
"studentId": 8,
"firstname": "omg",
"lastname": "yo",
"yearLevel":0
}
]