Reputation: 117
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>'
); });
});
});
Upvotes: 3
Views: 222
Reputation: 44992
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
Upvotes: 2
Reputation: 65254
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..
Upvotes: 0
Reputation: 8166
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"}]}
Upvotes: 1
Reputation: 10880
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');
Upvotes: 1