jQuery $ .getJSON()没有返回任何内容

时间:2013-09-27 13:42:57

标签: javascript jquery html json

我有一个脚本,我用它来尝试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>

但它没有任何显示。

1 个答案:

答案 0 :(得分: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);
      });
  }
相关问题