The JSON data from the URL given below is like this:
{
"datum":[
{
"id":"19",
"song":"The Age of Worry",
"path":"audio/age.mp3",
"artist":"John Mayer",
"album":"Born And Raised",
"genre":"Blues-Rock",
"year":"2012",
"filename":"age",
"extension":".mp3",
"size":"6.1539077758789 MB ",
"duration":"2:40",
"downloads":"0"
}
]
}
I want to alert the song name. the code below only alerts id but not the song. I couldn’t figure out what’s wrong here.
switch(id){
case "1":
alert(id);
$.getJSON("http://localhost/musicstore/search.php?media=audio&search_by=song&search_string=age", function(d) {
$.each(d.datum, function (i, res) {
alert(res.song);
});
});
break;
case 2:
break;
case 3:
break;
}
I added this fiddle. Your .each logic is working just fine assuming that your JSON data is coming back like your example says. So the problem must be in your PHP script returning the JSON.
Here is the simplified fiddle just to test your .each logic: