Logan Shire
Logan Shire

Reputation: 5103

jQuery .getJSON and .ajax get request both return null

I am trying to load a json file with ajax to populate my html, and for whatever reason, when I try to do so the json that is returned from either a simple ajax call or .getJSON call is null. Here is my HTML:

<!doctype html>
<html>
  <head>
     <title>Lab 4 AJAX</title>
     <script type="text/javascript" src="resources/jquery-1.4.3.min.js"></script>
     <script src="lab4.js"></script>
  </head>
  <body>
    <div id="song-template">
      <a id="site" href="#"><img id="coverart" src="images/noalbum.png"/></a>
      <h1 id="title"></h1>
      <h2 id="artist"></h2>
      <h2 id="album"></h2>
      <p id="date"></p>
      <p id="genre"></p>
    </div>
  </body>
</html>

Here is a sample of the JSON I'm trying to load:

[
    {
        "name" : "Short Skirt, Long Jacket",
        "artist" : "Cake",
        "album" : "Comfort Eagle",
        "genre" : "Rock",
        "year" : 2001,
        "albumCoverURL" : "images/ComfortEagle.jpg",
        "bandWebsiteURL" : "http://www.cakemusic.com"
    }
]

And here is the javascript I am trying to load it with:

function updateHTML(json) 
{
    var templateNode = $("#song-template").clone(true);
    $("#song-template").remove();

    debugger;

    $.each(json, function(key, song)
    {
        var songNode = templateNode.clone(true);
        songNode.children("#site").href(song.bandWebsiteURL);
        songNode.children("#coverart").src(song.albumCoverURL);
        songNode.children("#title").text(song.name);
        songNode.children("#artist").text(song.artist);
        songNode.children("#album").text(song.album);
        songNode.children("#date").text(song.year);
        songNode.children("#genre").text(song.genre);
        $("body").append(songNode);
    });
}

$(document).ready(function()
{
    $("#site").click(function() 
    {
        $.getJSON("lab4.json", updateHTML);
        // $.ajax(
        // {
        //  url: "lab4.json",
        //  type: "GET",
        //  dataType: "json",
        //  success: updateHTML
        // });
    });
});

Both the getJSON and the ajax result in json being null when updateHTML is run. Any idea what could be causing this?

Upvotes: 0

Views: 346

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191729

.href and .src are not jQuery methods. You need to write:

.attr("href", 
.attr("src", 

http://jsfiddle.net/ExplosionPIlls/cwwCh/

Upvotes: 1

Related Questions