0

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!

1 Answer 1

3

your json notation is wrong

use : instead of = like:

..........
"images" : [
    { "source" : "images1", "alternative" : "altImg1" },
    ....................
]
..........
Sign up to request clarification or add additional context in comments.

3 Comments

Oh, how dumb am I... Thanks a lot!
I guess jQuery gracefully cloackes the error. Try using a json validator to pick out small future errors jsonlint.com ;)
Yeah, I don't like web developing because of this(and the amount of code you need to write). Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.