Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON data not displayed using getJSON

Tags:

json

jquery

php

$('document').ready(function() 
    { 
        $.getJSON('news.php', parseInfo);
});

function parseInfo(data)
{
    alert(data.news);//undefined
    $('#info').html(data.news);
}

My PHP output(news.php) is [{"id":"20110428","news":"april 28 2011"}]

like image 842
Nitish Avatar asked Jan 27 '26 16:01

Nitish


1 Answers

Your object is wrapped inside an array.

You need data[0].news

Alternatively remove the outer square brackets from your JSON output.

like image 71
Alnitak Avatar answered Jan 30 '26 04:01

Alnitak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!