【发布时间】:2018-01-24 16:56:20
【问题描述】:
自从我刚开始学习以来,我有一个脚本用来对 jQuery 进行一些实验。
脚本应该读取 .json 文件并在 div 中显示一些数据。
function jQuerytest()
{
$.getJSON( "books/testbook/pageIndex.json", function(result) {
$.each(result, function(i, field) {
$("div").append("<p>" + field + "</p>");
});
});
}
这里是html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="styles/main.css" rel="stylesheet" type="text/css">
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="scripts/book.js"></script>
<script>jQuerytest();</script>
</head>
<body>
<div></div>
<figure id=fig><img onClick="jQuerytest()" src="figures/test620x620.png"/>
</figure>
</body>
</html>
但它不显示任何内容。
【问题讨论】:
-
在您选择的浏览器中打开您的调试工具并查看网络流量。确保请求
books/testbook/pageIndex.json以 HTTP/200 返回。 -
如果得到 200 状态码和响应,则只需在
jQuerytest()末尾添加return false;语句 -
并检查您的 JSON 是否有效,使用
.fail(function () { console.log(arguments); })。如果您的 json 不正确,您将收到parse error。 -
好的,我尝试了 .fail() 和 .error() ,结果它确实失败了。所以我查看了我的 json 文件,它看起来不错,我将它复制到 JSON 验证器中,它说它很好。所以我从头开始创建了一个新的 json 文件并将 json 复制到其中并尝试使用该文件,现在由于某种原因它正在工作..
标签: javascript jquery html json