PHP and JSON help

Thank you for watching

My problem is that I cannot 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>'
); });      
});
    });
+3
source share
4 answers

I just tried checking your JSON using http://www.jsonlint.com/

And this failed:

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

The general (external) brackets should be changed from (and )to {and }This will confirm your JSON, and the script should work fine

+2
source

I did not check your syntax for you json, but if it is correct try this before sending the output (to a PHP file)

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

Json , . :

{"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"}]}
+1

json

{
    "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" 
        }
    ]
}

jsonLint json..

0

Source: https://habr.com/ru/post/1750242/


All Articles