【发布时间】:2017-05-19 06:56:49
【问题描述】:
我正在使用 Untappd API 构建啤酒菜单,我有一个问题。
我想显示服务器返回给我的 JSON 数据,并将其放入 HTML 表格中。
当我创建一个 list.json 文件并通过我的代码将其导入时,它可以正常工作,但是每当我尝试使用 URL 本身时,它都不会提取数据。谁能帮我解决这个问题?
下面的代码通过调用本地 json 文件来工作,而不是通过 URL。
json
"items": [
{
"id": xxx,
"section_id": xxx,
"position": x,
"untappd_id": xxx,
"label_image": "xxx",
"brewery_location": "xxx",
"abv": "xx",
"ibu": "xx",
"cask": xx
},
{
"id": xxx,
"section_id": xxx,
"position": x,
"untappd_id": xxx,
"label_image": "xxx",
"brewery_location": "xxx",
"abv": "xx",
"ibu": "xx",
"cask": xx
},
...
HTML/JS
<html>
<head>
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>
<script>
$(function() {
var beer = [];
$.getJSON('test.json', function(data) {
$.each(data.items, function(i, f) {
var tblRow = "<tr>" + "<td>" + f.name + "</td>" +
"<td>" + f.brewery + "</td>" + "<td>" + f.ibu + "</td>" + "<td>" + f.abv + "</td>" + "</tr>"
$(tblRow).appendTo("#userdata tbody");
});
});
});
</script>
</head>
<body>
<div class="wrapper">
<div class="profile">
<table id="userdata" border="2">
<thead>
<th>Beer Name</th>
<th>Brewery</th>
<th>IBU</th>
<th>ABV</th>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</body>
</html>
任何帮助都会很棒!
【问题讨论】:
-
为什么你有两个
jquery的来源?
标签: javascript html json api html-table