1

自从我刚开始学习以来,我有一个脚本用于对 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>

但它不显示任何内容。

4

1 回答 1

6

如果您的 JSON 文件有效(您可以在这里测试:http: //jsonlint.com/)使用成功和错误回调来最终获取它为什么不工作的信息。

function jQuerytest(){
    $
      .getJSON( "books/testbook/pageIndex.json")
      .success(function(result) {
          $.each(result, function(i, field) {
               $("div")
                .append("<p>" + field + "</p>");
          });
      })
      .error(function(error){
          console.log(error);
      });
  }
于 2013-09-27T13:44:45.117 回答