我正在使用this jQuery plugin (fiddle)来阅读Google新闻RSS Feed。它需要将feed转换为json格式。然后我遇到this thread,它显示了JSON格式的Google Feed,没有Yahoo Pipe的帮助:
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 Feed,但失败了。任何人都可以告诉我阅读该Feed的正确方法吗?
我的尝试:
<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>
Google新闻Feed:
{"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://....
答案 0 :(得分:3)
由于 Same-origin policy ,您的代码无效。
一种可能的解决方案是使用Google新闻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 强>