我对 javascript 很陌生,我想使用 javascript 显示 json 文件的内容。我的问题是html页面无法显示Json文件的内容并且它是空白的!我知道我正确地获取了它。我的本地驱动器上有这个 Json 文件,名称为 data.json。我也更改了文件的地址,但它不起作用。
你能告诉我这是什么原因吗? 谢谢,
我把代码放在这里
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"> </script>
<script>
$(function() {
var people = [];
$.getJSON("home/Documents/data.json", function(data) {
$.each(data.person, function(i, f) {
var tblRow = "<tr>" + "<td>" + f.firstName + "</td>" +
"<td>" + f.lastName + "</td>" + "<td>" + f.job + "</td>" + "<td>" + f.roll + "</td>" + "</tr>"
$(tblRow).appendTo("#userdata tbody");
});
});
});
</script>
</head>
<body>
<div class="wrapper">
<div class="profile">
<table id= "userdata" border="2">
<thead>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>City</th>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</body>
</html>
最佳答案
I have this Json file on my local drive
不要尝试在本地磁盘上执行 Ajax,您会遇到浏览器安全限制。运行 Web 服务器并通过 HTTP 进行操作。
您应该通过查看浏览器的开发人员工具中的 JavaScript 错误控制台来了解这一点。
关于javascript - 为什么Json文件内容不显示在html页面中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17190517/