www.webdeveloper.com
Results 1 to 4 of 4

Thread: Problem with retreiving JSON data by jquery

  1. #1
    Join Date
    Dec 2012
    Posts
    4

    Problem with retreiving JSON data by jquery

    HI, this is my first post.

    I am working on the book "Learning jQuery, third ed". I followed the example retrieving a json file and display it on the webpage, but it didn't work.

    I even copied the example code and it didn't work either, anyone can help?

    The code is very simple:

    Code:
    $(document).ready(function() {
      $('#letter-a a').click(function() {
        $('#dictionary').load('a.html');
        return false;
      });
    
      $('#letter-b a').click(function() {
        $.getJSON('b.json', function(data) {
          var html = '';
          $.each(data, function(entryIndex, entry) {
            html += '<div class="entry">';
            html += '<h3 class="term">' + entry.term + '</h3>';
            html += '<div class="part">' + entry.part + '</div>';
            html += '<div class="definition">';
            html += entry.definition;
            html += '</div>';
            html += '</div>';
          });
          $('#dictionary').html(html);
        });
        return false;
      });
    });

  2. #2
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    Well, I don't notice anything odd with the code. Do you get any errors or warnings? Does b.json exist in the same directory as your HTML document? Maybe your JSON data has something wrong in it?

  3. #3
    Join Date
    Dec 2012
    Posts
    4
    I cannot find any problem either, here is the json file.

    [
    {
    "term": "BACCHUS",
    "part": "n.",
    "definition": "A convenient deity invented by the ancients as an excuse for getting drunk.",
    "quote": [
    "Is public worship, then, a sin,",
    "That for devotions paid to Bacchus",
    "The lictors dare to run us in,",
    "And resolutely thump and whack us?"
    ],
    "author": "Jorace"
    },
    {
    "term": "BACKBITE",
    "part": "v.t.",
    "definition": "To speak of a man as you find him when he can't find you."
    },
    {
    "term": "BEARD",
    "part": "n.",
    "definition": "The hair that is commonly cut off by those who justly execrate the absurd Chinese custom of shaving the head."
    },
    {
    "term": "BEGGAR",
    "part": "n.",
    "definition": "One who has relied on the assistance of his friends."
    },
    {
    "term": "BELLADONNA",
    "part": "n.",
    "definition": "In Italian a beautiful lady; in English a deadly poison. A striking example of the essential identity of the two tongues."
    },
    {
    "term": "BIGAMY",
    "part": "n.",
    "definition": "A mistake in taste for which the wisdom of the future will adjudge a punishment called trigamy."
    },
    {
    "term": "BORE",
    "part": "n.",
    "definition": "A person who talks when you wish him to listen."
    }
    ]

  4. #4
    Join Date
    Dec 2012
    Posts
    4
    It looks like both the code and the json file are fine.

    I checked my console, and it said, "Failed to load resource: the server responded with a status of 404 (Not Found) "

    the file is THERE but the code could not get access to it, anyone could help? It is a IIS server on my windows 7.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles