簡體   English   中英

jQuery $ .getJSON()不返回任何內容

[英]jQuery $.getJSON() not returning anything

自從我剛開始學習以來,我就有了一個腳本來嘗試使用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>

但它不顯示任何內容。

如果您的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);
      });
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM