使用 jQuery 解析 RSS feed,在 Firefox 和 Chrome 中遇到问题
我使用了一个名为 jFeed 的 jQuery 库来解析并在我的个人网站上显示我的博客 rss feed。一开始它工作得很好,但后来检查时它什么也没显示,除了在 Internet Explorer 中,它似乎工作得很好。
在 Firefox 中使用 Firebug 检查 JavaScript 控制台后,“XML”选项卡中显示错误,如下所示:
XML 解析错误:找不到元素位置:moz-nullprincipal:{3f8a0c62-32b4-4f63-b69c- 9ef402b40b64} 第 1 行,第 1 列: ^
虽然我不知道如何处理这些信息。这是我用来获取 rss feed 并显示它的代码(它与 jFeed 网站提供的示例几乎完全相同):
jQuery.getFeed({ url: 'http://sammarshalldesign.co.uk/blog/wordpress/?feed =rss2', 成功:函数(提要){
var html = '';
for(var i = 0; i < feed.items.length && i < 5; i++) {
var item = feed.items[i];
html += '<h3>'
+ '<a href="'
+ item.link
+ '">'
+ item.title
+ '</a>'
+ '</h3>';
html += '<div>'
+ item.description
+ '</div>';
}//end for
jQuery('#feed').append(html);
}//end feed function
});//end getfeed
任何帮助将不胜感激。
I used a jQuery library called jFeed to parse and display my blogs rss feed on my personal website. It worked perfectly well at first, but upon checking later it simply displays nothing, except in Internet Explorer, where it seems to work fine.
After checking the javascript console using Firebug in Firefox, it shows an error in the 'XML' tab as follows:
XML Parsing Error: no element found Location: moz-nullprincipal:{3f8a0c62-32b4-4f63-b69c- 9ef402b40b64} Line Number 1, Column 1:
^
Though I have no idea what to do with this information. Here is the code I used to get the rss feed and display it (it is almost exactly the same as the example provided by the jFeed website):
jQuery.getFeed({
url: 'http://sammarshalldesign.co.uk/blog/wordpress/?feed=rss2',
success: function(feed) {
var html = '';
for(var i = 0; i < feed.items.length && i < 5; i++) {
var item = feed.items[i];
html += '<h3>'
+ '<a href="'
+ item.link
+ '">'
+ item.title
+ '</a>'
+ '</h3>';
html += '<div>'
+ item.description
+ '</div>';
}//end for
jQuery('#feed').append(html);
}//end feed function
});//end getfeed
Any help would be really appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作为替代方案,Google 确实提供了 Feed api。看看这个 Google Feed 插件,它使整个过程变得非常简单(不需要 PHP)。
As an alternative, Google does provide a feed api. Check out this Google Feed Plugin that makes the whole process pretty easy (no php required).
您是否正在尝试从另一个域加载 RSS 提要?如果是这样,那就行不通了。 jFeed 附带一个示例 PHP 代理,您可以将其放置在服务器上并调用。或者,您可以使用雅虎!通过管道获取 JSON 格式的数据。
Are you trying to load the RSS feed from another domain? If so, it won't work. jFeed comes with a sample PHP proxy that you can place on your server and call. Or, you can use Yahoo! Pipes to get the data in JSON format.
我通过艰难的方式发现了这一点,但与 Internet Explorer 不同,Firefox 不允许跨域 XML 请求,除非您正在访问的服务器进行某种身份验证。您需要使用 JSON(您可以使用 JSON)在 jQuery 中执行跨站点/跨域请求),或者在本地为 XML feed 创建某种代理,然后使用 jQuery 请求来命中该代理。
I discovered this the hard way, but unlike Internet Explorer, Firefox doesn't allow for cross-domain XML requests without some kind of authentication from the server you're hitting.. you'll need to either use JSON (with which you can do a cross-site / cross-domain request in jQuery), or create some kind of proxy for your XML feed locally and then hit that with your jQuery request.