【发布时间】:2018-03-26 20:08:41
【问题描述】:
我正在使用 Json Data 用详细信息列表填充我的 Html 表。我在 index.html 页面所在的同一目录中有名为 example.json 的 json 文件。但是当我在下面的代码中添加“example.json”时它不会显示/读取 json 文件,但是当我加载在线 url 时工作得很好。 我想用本地存储中的 Json 文件填充表,并在在线时自动用新数据覆盖 example.json 的先前数据。
这是我的 HTML
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="table-responsive">
<h1>Json</h1>
<br/>
<table class="table table-bordered table-striped" id="employee_table">
<tr>
<th>Class</th>
<th>Time</th>
</tr>
</table>
</div>
</div>
</body>
</html>
<script >
$(document).ready(function(){
$.getJSON("https://api.myjson.com/bins/8qktd", function(data){
var employee_data= '';
$.each(data, function(key, value){
employee_data += '<tr>';
employee_data += '<td>'+value.class+'</td>';
employee_data += '<td>'+value.time+'</td>';
employee_data += '</tr>';
});
$('#employee_table').append(employee_data);
});
});
</script>
一个
[
{
"id":"1",
"time":"10-15",
"class":"John Smith"
},
{
"id":"2",
"time":"10-15",
"class":"John Smith"
},
{
"id":"3",
"time":"10-15",
"class":"John Smith"
},
{
"id":"4",
"time":"10-15",
"class":"John Smith"
},
{
"id":"5",
"time":"10-15",
"class":"John Smith"
},
{
"id":"6",
"time":"10-15",
"class":"John Smith"
},
{
"id":"7",
"time":"10-15",
"class":"John Smith"
},
{
"id":"8",
"time":"10-15",
"class":"John Smith"
},
{
"id":"9",
"time":"10-15",
"class":"John Smith"
},
{
"id":"10",
"time":"10-15",
"class":"John Smith"
},
{
"id":"11",
"time":"10-15",
"class":"John Smith"
},
{
"id":"12",
"time":"10-15",
"class":"John Smith"
}
]
非常感谢任何帮助。我已经在寻找解决方案,但无处可寻。
【问题讨论】:
-
使用本地网络服务器(如 xampp)来托管您的网站。这是一种很好的做法,它也可以解决这些类型的问题。
-
你在哪个浏览器上尝试代码?
-
我在 google chrome 上使用它,稍后我打算在 android Webview 中使用它。
-
@guest271314,我真的很感谢你的努力,但现在我正试图让它在桌面浏览器中工作,然后只有我会进入 webview。现在,我遇到了问题,不知道如何离线加载 json 文件(在线工作很好)但是当我切断互联网时,它没有显示任何数据。
标签: javascript jquery json html