javascript - 如何使用 JavaScript(外部域)解析 RSS 提要?

标签 javascript jquery xml rss

问题

我需要解析 RSS 提要并在 HTML 页面中显示解析的详细信息。

我找到的解决方案

How to parse an RSS feed using JavaScript?是一个非常相似的问题,我跟着它。

使用上述问题,我构建了以下代码。

 <script>
  $(document).ready(function() {
    //feed to parse
    var feed = "https://feeds.feedburner.com/raymondcamdensblog?format=xml";

    $.ajax(feed, {
        accepts:{
            xml:"application/rss+xml"
        },
        dataType:"xml",
        success:function(data) {
            //Credit: http://stackoverflow.com/questions/10943544/how-to-parse-an-rss-feed-using-javascript

            $(data).find("item").each(function () { // or "item" or whatever suits your feed
                var el = $(this);
                document.write("------------------------");
                document.write("title      : " + el.find("title").text());
                document.write("link       : " + el.find("link").text());
                document.write("description: " + el.find("description").text());
            });


        }   
    });

});
</script>

错误

Failed to load https://feeds.feedburner.com/raymondcamdensblog?format=xml: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

我需要什么

如何更改我的代码以使用 JavaScript 读取 RSS 提要而不会出现上述错误?

最佳答案

你可以使用类似 https://rss2json.com 的东西. 它将 feed 解析为 json 以获取 javascript:

var feedURL = "https://feeds.feedburner.com/raymondcamdensblog?format=xml";
$.ajax({
  type: 'GET',
  url: "https://api.rss2json.com/v1/api.json?rss_url=" + feedURL,
  dataType: 'jsonp',
  success: function(result) {
    console.log(result);
  }
});

关于javascript - 如何使用 JavaScript(外部域)解析 RSS 提要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51647764/

相关文章:

javascript - jsperf 替代方案适用于 IE 吗?

javascript - 如何将我的 DIV 放置在一个独特的网格中?

java - VTD-XML 可以将字符串作为输入吗?

javascript - 如何使用带标志的语言获取下拉列表当前值?

javascript - 寻求有关异步javascript的澄清

javascript - 如何将字段数据从一种格式更改为另一种格式

jquery - 使用 CSS 的 iTunes 样式布局

jquery - 使用 jquery 显示不同尺寸的随机图像

html - Rails 应用程序上的奇怪错误,仅在一个浏览器上,一个 url "Below is a rendering of the page up to the first error."

python - 如何获取qt中特定xml节点的所有属性