我试图通过尝试阅读RSS源然后在单个网页上显示输出来尝试学习一些Web开发。
目前,当我尝试阅读RSS源时,收到错误消息:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.URL.com/rss.php?secret_key=*******&feedtype=download&timezone=0&showrows=50&categories=53,3,5. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
阅读类似的帖子here,我认为答案是添加标题,如下面的代码所示,但这不起作用。
为什么我收到此错误以及如何解决此问题?
代码:
<!DOCTYPE html>
<html>
<head>
<title>RSS Reader</title>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
</head>
<script>
$(document).ready(function() {
//feed to parse
var feed = "https://www.URL.com/rss.php?secret_key=*******&feedtype=download&timezone=0&showrows=50&categories=53,3,5";
$.ajax(feed, {
accepts:{
xml:"application/rss+xml"
},
dataType:"xml",
headers: {
"Access-Control-Allow-Headers":"x-requested-with"
},
success:function(data) {
$(data).find("item").each(function () {
var el = $(this);
console.log("------------------------");
console.log("title : " + el.find("title").text());
console.log("link : " + el.find("link").text());
console.log("description: " + el.find("description").text());
});
}
});
});
</script>
<body>
this is a test page
</body>
</html>