【问题标题】:Use jQuery to read a JSON Google news feed使用 jQuery 读取 JSON 谷歌新闻提要
【发布时间】:2014-03-08 19:21:52
【问题描述】:

我正在使用this jQuery plugin (fiddle) 来阅读 Google 新闻 rss 提要。它需要将提要转换为 json 格式。然后我遇到了this thread,它在没有 Yahoo Pipe 帮助的情况下以 JSON 格式显示 Google 提要:

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

我尝试了插件的方法来解析 JSON Google 提要,但失败了。谁能告诉我阅读该提要的正确方法?

我的尝试:

<script>
$('#rssdata').ready(function()
{
    var pipe_url = 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=8&q=http%3A%2F%2Fnews.google.com%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>

谷歌动态消息:

{"responseData": {"feed":{"feedUrl":"http://news.google.com/news?output\u003drss\u0026num\u003d8","title":"Top Stories - Google News","link":"http://news.google.com/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://....

【问题讨论】:

  • 这很棒。我想从谷歌获取与一家公司相关的新闻提要,比如微软。如何仅获取与 Microsoft 相关的新闻提要?

标签: javascript jquery json parsing rss


【解决方案1】:

由于 Same-origin policy,您的代码无法正常工作。

一种可能的解决方案是使用受 Google News Feed API 支持的JSONP

所以你可以这样做:

$('#rssdata').ready(function () {
    $.ajax({
        url: 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=8&q=http%3A%2F%2Fnews.google.com%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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-27
相关资源
最近更新 更多