我在 http://localhost:8080/CRUDWebAppMavenized/persons
上有一个静态 Web 服务,它输出一个 json 数组。我在 jquery 中使用 AJAX,我想在 HTML 表中显示 json 数组。但我无法让它显示在 html 表中。我将发布 html、javascript 代码和 json 数组代码。
index3.html HTML 文件。用于在浏览器中显示学生信息。
<!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(使用 Jquery) AJAX调用,然后将代码放在html文件的table标签中。
$(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 数组 来自 Web 服务 URL 的 JSON 数组。这是由网络应用程序生成的。
[
{
"studentId": 5,
"firstname": "bro",
"lastname": "oy",
"yearLevel": 0
},
{
"studentId": 6,
"firstname": "lol",
"lastname": "keke",
"yearLevel": 0
},
{
"studentId": 8,
"firstname": "omg",
"lastname": "yo",
"yearLevel":0
}
]
最佳答案
首先 jquery
然后是 jquery 依赖的 js
就像
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="engine.js"></script>
关于javascript - 在 JQuery 中使用 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22832231/