Reputation: 287
I'm trying to make jQuery take JSON file and put the data from it on a simple site, when a button is pressed. So, the JSON code looks like this:
{
"images" : [
{ "source" = "images1", "alternative" = "altImg1" },
{ "source" = "images2", "alternative" = "altImg2" },
{ "source" = "images3", "alternative" = "altImg3" }
]
}
And the HTML + jQuery:
<html xmlns="http://www.w3.org/1999/xhtml">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<title>jQuery</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<button>Press Me!</button>
<script>
$('button').click(function() {
$.getJSON('json-db.html', function(data) {
for(var i = 0; i < data.images.length; i++) {
var image = data.images[i];
$('#result').append('<h1>' + image.source + ' ' + image.alternative + '</h1>');
}
});
});
</script>
<div id="result">Result</div>
</body>
</html>
There are no errors detected by Firebug. I rewrote the code several times, looked for mistakes, compared it to a similar code and so on, but couldn't find anything.
Thanks in advance!
Upvotes: 0
Views: 559
Reputation: 21864
your json notation is wrong
use :
instead of =
like:
..........
"images" : [
{ "source" : "images1", "alternative" : "altImg1" },
....................
]
..........
Upvotes: 3