tags:

views:

51

answers:

4

hi, guys

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  A: 

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

Lizard
+1  A: 

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');
atif089
+1  A: 

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"}]}
mamoo
A: 

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..

Reigel