我目前正在尝试学习 RESTful API 并将它们实现到用例中,我想做的一件事是将带有 json 有效负载的 url 从一台服务器加载到单独的 Web 服务器中,以在表上显示数据。我对此不太熟悉,所以我试图找到最好的方法来做到这一点。
我正在使用此 API 发布到domain.com/todos 页面
https://github.com/corylanou/tns-restful-json-api https://github.com/corylanou/tns-restful-json-api
然后我尝试用它将其打印到表格中https://github.com/sam-suresh/JSON-URL-to-HTML-Table https://github.com/sam-suresh/JSON-URL-to-HTML-Table
但它看起来并没有起作用。我将其全部放入一个索引文件中,它显示它在控制台上点击了我的 api,但我没有在表中显示任何输出。
<html>
<table id="personDataTable">
<tr>
<th>id</th>
<th>name</th>
<th>due</th>
</tr>
</table>
<style>
table {
border: 2px solid #666;
width: 100%;
}
th {
background: #f8f8f8;
font-weight: bold;
padding: 2px;
}
</style>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
$.ajax({
url: 'http://my-website-domain.com/todos',
type: "get",
dataType: "json",
success: function(data) {
drawTable(data);
}
});
function drawTable(data) {
for (var i = 0; i < data.length; i++) {
drawRow(data[i]);
}
}
function drawRow(rowData) {
var row = $("<tr />")
$("#personDataTable").append(row);
row.append($("<td>" + rowData.id + "</td>"));
row.append($("<td>" + rowData.name + "</td>"));
row.append($("<td>" + rowData.due + "</td>"));
}
</script>
</html>
这就是 /todos 页面上显示的内容
[{"id":1,"name":"Write presentation","completed":false,"due":"0001-01-01T00:00:00Z"},{"id":2,"name":"Host meetup","completed":false,"due":"0001-01-01T00:00:00Z"},{"id":3,"name":"New Todo","completed":false,"due":"0001-01-01T00:00:00Z"}