3

thanks for looking

my problem is that i cant seem to get jquery to display my generated data.

here is my JSON output

("posts":[{"id":"1-2","time":"0","name":"dash","avatar":"http:\/\/www.gravatar.com\/avatar\/9ff30cc2646099e31a4ee4c0376091b0?s=182&d=identicon&r=PG","comment":"rtetretrete tet rt uh utert"},{"id":"2-2","time":"0","name":"james","avatar":"http:\/\/www.gravatar.com\/avatar\/d41d8cd98f00b204e9800998ecf8427e?s=182&d=identicon&r=PG","comment":"fsdfdfsdf\r\n"}])

and here is my jquery

    $(document).ready(function(){
        var url="comments.php";
        $.getJSON(url,function(json){
            $.each(json.posts,function(i,post){
$("#content").append(
    '<div class="post">'+
        '<h1>'+post.name+'</h1>'+
        '<p>'+post.comment+'</p>'+
        '<p>added: <em>'+post.time+'</em></p>'+
        '<p>posted by: <strong>'+post.name+'</strong></p>'+
        '<p>avatar: <strong>'+post.avatar+'</strong></p>'+
    '</div>'
); });      
});
    });
2
  • 1
    What does this have to do with PHP? Commented Jun 16, 2010 at 8:22
  • The json is being generated by php. The actual problem isn't related to that but it is involved. Commented Jun 17, 2010 at 12:24

4 Answers 4

2

I have just tried to validate your JSON using http://www.jsonlint.com/

And it failed:

syntax error, unexpected TINVALID, expecting '{' or '[' at line 1
Parsing failed

The overall (outside) brackets need to be changed from ( and ) to { and } This will validate your JSON and the script should work fine

Sign up to request clarification or add additional context in comments.

Comments

1

I didnt check your syntax for you json, but if it is correct then try this before sending the output (in PHP file)

header ('Content-type: application/json');

Comments

1

Your Json object is missing leading and trailing curly braces, thus is not valid. Try to add them:

{"posts":[{"id":"1-2","time":"0","name":"dash","avatar":"http:\/\/www.gravatar.com\/avatar\/9ff30cc2646099e31a4ee4c0376091b0?s=182&d=identicon&r=PG","comment":"rtetretrete tet rt uh utert"},{"id":"2-2","time":"0","name":"james","avatar":"http:\/\/www.gravatar.com\/avatar\/d41d8cd98f00b204e9800998ecf8427e?s=182&d=identicon&r=PG","comment":"fsdfdfsdf\r\n"}]}

Comments

0

json should be

{
    "posts": [
        {
            "id": "1-2",
            "time": "0",
            "name": "dash",
            "avatar": "http:\/\/www.gravatar.com\/avatar\/9ff30cc2646099e31a4ee4c0376091b0?s=182&d=identicon&r=PG",
            "comment": "rtetretrete tet rt uh utert" 
        },
        {
            "id": "2-2",
            "time": "0",
            "name": "james",
            "avatar": "http:\/\/www.gravatar.com\/avatar\/d41d8cd98f00b204e9800998ecf8427e?s=182&d=identicon&r=PG",
            "comment": "fsdfdfsdf\r\n" 
        }
    ]
}

use jsonLint to validate json..

Comments

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.