问题
我需要解析 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/