Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery $.getJSON() not returning anything

I have a script that i'm using to experiment a bit with jQuery since i just started learning.

the script is supposed to read a .json file and display some data in a div.

function jQuerytest()
{
    $.getJSON( "books/testbook/pageIndex.json", function(result) {
        $.each(result, function(i, field) {
            $("div").append("<p>" + field + "</p>");
        });
    });
}

here is the 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>

but it doens't display anything.

like image 766
Kevin Oonk Avatar asked Sep 11 '25 00:09

Kevin Oonk


1 Answers

If your JSON File is valid (You can test here: http://jsonlint.com/) Use the success and error callback to finally get information why it's not working.

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);
      });
  }
like image 96
Bernhard Avatar answered Sep 13 '25 14:09

Bernhard