最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Use jQuery to read a JSON Google news feed - Stack Overflow

matteradmin10PV0评论

I'm using this jQuery plugin (fiddle) to read Google news rss feed. It requires converting the feed to json format. Then I came across this thread that shows a Google feed in JSON format without the help of Yahoo Pipe:

.0&q=http%3A%2F%2Fnews.google%2Fnews%3Foutput%3Drss%26num%3D8

I tried the plugin's method to parse the JSON Google feed but failed. Can anyone show me the correct way to read that feed?

My attempt:

<script>
$('#rssdata').ready(function()
{
    var pipe_url = '.0&num=8&q=http%3A%2F%2Fnews.google%2Fnews%3Foutput%3Drss';
    $.getJSON(pipe_url,function(data)
    {
        $(data.feed.entries).each(function(index,entry)
        {

            var item_html = '<li><a target="_blank" href="'+entry.link+'">'+entry.title+'</a></li>';
            $('#rssdata ul.rss-items').append(item_html);
        });
        $('#rssdata div.loading').fadeOut();
        $('#rssdata ul.rss-items').slideDown();
    });
});
</script>

Google News Feed:

{"responseData": {"feed":{"feedUrl":"\u003drss\u0026num\u003d8","title":"Top Stories - Google News","link":"\u003d1\u0026amp;ned\u003dus\u0026amp;hl\u003den\u0026amp;num\u003d8","author":"","description":"Google News","type":"rss20","entries":[{"title":"Malaysia Airlines loses contact with plane en route to Beijing with 239 aboard - CBS News","link":"http://....

I'm using this jQuery plugin (fiddle) to read Google news rss feed. It requires converting the feed to json format. Then I came across this thread that shows a Google feed in JSON format without the help of Yahoo Pipe:

http://ajax.googleapis./ajax/services/feed/load?v=1.0&q=http%3A%2F%2Fnews.google.%2Fnews%3Foutput%3Drss%26num%3D8

I tried the plugin's method to parse the JSON Google feed but failed. Can anyone show me the correct way to read that feed?

My attempt:

<script>
$('#rssdata').ready(function()
{
    var pipe_url = 'http://ajax.googleapis./ajax/services/feed/load?v=1.0&num=8&q=http%3A%2F%2Fnews.google.%2Fnews%3Foutput%3Drss';
    $.getJSON(pipe_url,function(data)
    {
        $(data.feed.entries).each(function(index,entry)
        {

            var item_html = '<li><a target="_blank" href="'+entry.link+'">'+entry.title+'</a></li>';
            $('#rssdata ul.rss-items').append(item_html);
        });
        $('#rssdata div.loading').fadeOut();
        $('#rssdata ul.rss-items').slideDown();
    });
});
</script>

Google News Feed:

{"responseData": {"feed":{"feedUrl":"http://news.google./news?output\u003drss\u0026num\u003d8","title":"Top Stories - Google News","link":"http://news.google./news?pz\u003d1\u0026amp;ned\u003dus\u0026amp;hl\u003den\u0026amp;num\u003d8","author":"","description":"Google News","type":"rss20","entries":[{"title":"Malaysia Airlines loses contact with plane en route to Beijing with 239 aboard - CBS News","link":"http://....
Share Improve this question edited May 23, 2017 at 12:15 CommunityBot 11 silver badge asked Mar 8, 2014 at 19:21 RedGiantRedGiant 4,76911 gold badges63 silver badges151 bronze badges 1
  • This is great. I want to get the news feed from google which is related to one pany lets say as Microsoft. How to get only news feed related to only Microsoft? – Mihir Commented Oct 16, 2014 at 9:39
Add a ment  | 

1 Answer 1

Reset to default 3

Your code is not working because of the Same-origin policy.

One possible solution is to use JSONP which is supported by Google News Feed API.

So you can do:

$('#rssdata').ready(function () {
    $.ajax({
        url: 'http://ajax.googleapis./ajax/services/feed/load?v=1.0&num=8&q=http%3A%2F%2Fnews.google.%2Fnews%3Foutput%3Drss',
        dataType: 'jsonp',
        success: function (data) {
            //console.log(data.feed.entries);
            $(data.responseData.feed.entries).each(function (index, entry) {
                var item_html = '<li><a target="_blank" href="' + entry.link + '">' + entry.title + '</a></li>';
                $('#rssdata ul.rss-items').append(item_html);
            });
            $('#rssdata div.loading').fadeOut();
            $('#rssdata ul.rss-items').slideDown();
        },
        error: function () {}

    });
});

Updated Fiddle

Post a comment

comment list (0)

  1. No comments so far