Failed to load https://feeds.bbci.co.uk/news/rss.xml: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
我的代码如下所示:
fetch("https://feeds.bbci.co.uk/news/rss.xml")
.then(res => res.json())
.then(res => console.log(res))
我几乎尝试了一切。设置标题,设置模式,但没有任何效果。问题出在哪里?
您不被允许这样做,因为BBC网站没有设置正确的CORS标头。
仅当设置了正确的 CORS 标头时,您才能从其他域发出请求。您最好的选择是通过服务器端代理执行此操作,或者在 BBC 找到一份工作并尝试添加正确的标头。
您可以尝试改用 RSS2json:
google.load("feeds", "1");
google.setOnLoadCallback(() => {
const url = 'https://feeds.bbci.co.uk/news/rss.xml';
new google.feeds.Feed(url).load(function(result) {
!result.error && console.log(result.feed.entries);
});
});
<script type="text/javascript" src="https://rss2json.com/gfapi.js"></script>