为什么提取供稿时出现以下错误?

时间:2018-10-22 20:24:12

标签: javascript

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))

我几乎尝试了所有事情。设置标题,设置模式,但没有任何效果。有什么问题吗?

2 个答案:

答案 0 :(得分:0)

您不允许这样做,因为BBC网站未设置正确的CORS标头。

只有设置了正确的CORS标头后,您才能从其他域发出请求。最好的选择是通过服务器端代理来执行此操作,或者在BBC获得一份工作并尝试添加正确的标题。

答案 1 :(得分:-1)

您可以改用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>