【问题标题】:Jquery RSS feed to HTMLJquery RSS 提要到 HTML
【发布时间】:2016-11-17 06:47:53
【问题描述】:

所以我有一个 RSS 提要可以正确打印到控制台,没有错误。我可以看到在控制台中解析的所有 XML 都非常整洁。当我尝试在我的 HTML 中显示时,我看到的只是“未定义”。这是我的代码:

<script>
$.ajax({
    url      : document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeURIComponent('https://news.google.com/news?cf=all&hl=en&pz=1&ned=us&output=rss'),
    dataType : 'json',
    success  : function (data) {
        if (data.responseData.feed && data.responseData.feed.entries) {
            $.each(data.responseData.feed.entries, function (i, e) {
                console.log("------------------------");
                console.log("title      : " + e.title);
                console.log("author     : " + e.author);
                console.log("description: " + e.description);
                console.log("link: " + e.link);
            });
        }
        $('#rss-viewer')[0].innerHTML = data.innerHTML;
    }

});</script>  

<div id="rss-viewer"></div>

非常感谢任何帮助。

【问题讨论】:

  • 哦,要成为跨浏览器,您需要删除我用于调试的几行代码。基本上只是删除所有 console.log 行,因为它们不兼容 IE。
  • 另外,$('#rss-viewer')[0].innerHTML = 最好用 jQuery 写成 `$('#rss-viewer').html('')
  • 尝试将e 直接传递给console.log,看看你是否得到了你认为你得到的数据结构。
  • 当我执行 'data = ("title : " + e.title);' 时,我实际上得到了一些东西然后'$('#rss-viewer').html(data);'但它只给了我最后一个标题。

标签: jquery html json parsing rss


【解决方案1】:

返回时没有data.innerHTMLdata 具有属性 .responseDate.responseDetails.responseStatus

除此之外,正如评论中提到的,我在几年前写了this plugin,这使得使用 jQuery 阅读 RSS 变得非常容易。我最近将它发布到 get hub 并对其进行了一些更改,因此它更易于使用。下面,您将看到一个使用您的链接和一些非常简单的 jQuery 的简单示例,用于从返回的提要创建元素并将它们添加到 DOM。

Find On GitHub

最基本的使用:$.jQRSS('http://www.yourRSSurl.com/', { options }, function (newsFeed, feedEntries) { /* do work! */ })

示例:

$.jQRSS('https://news.google.com/news?cf=all&hl=en&pz=1&ned=us&output=rss', { count: 8 }, function (feed, entries) {
	console.log('feed:', $(feed));
	$.each(entries, function(i) {
		if (this['content']) {
			var fieldset = $('<fieldset/>', { title: this.contentSnippet }).appendTo('body'),
			legend = $('<legend/>').appendTo(fieldset),
			$link = $('<a />', { href: this.link, html: this.title, target: '_blank' }).appendTo(legend),
			$date = $('<h5 />', { html: this.publishedDate, style: 'float:right;' }).appendTo(fieldset),
			$content = $(this.content).appendTo(fieldset);
			console.log('entry '+i+':', $(this));
		}
	});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://rawgit.com/JDMcKinstry/jQRSS/master/jQRSS.js"></script>

【讨论】:

  • 为什么要投反对票?这只是另一种方式,并提供ease of use。要么接受,要么离开。
猜你喜欢
  • 2012-07-06
  • 2012-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-08
相关资源
最近更新 更多