ReactJS RSS Feed到Json转换器

时间:2019-06-15 14:29:17

标签: reactjs rss

我正在使用以下代码将RSS feed转换为JSON文件:

import React from "react";
import Button from '@material-ui/core/Button';
import Feed from 'rss-to-json'

const App = () => {    

const getFeed = () => {
  Feed.load('https://www.reddit.com/.rss', function(err, rss){
    console.log(rss);
    });
}


  return(
    <Button
      onClick = {() => getFeed()}
       variant="outlined">
      Submit
    </Button>
  ) 

}

export default App;

但是我在cosole中遇到以下错误:

Access to fetch at 'https://www.reddit.com/.rss' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

我不确定如何解决此错误。

1 个答案:

答案 0 :(得分:1)

当您访问上面的rss网址时,我认为您可能需要设置OAUTH进行身份验证。其他端点工作正常。看到这里-https://codesandbox.io/s/cocky-worker-u8zo9

您可以获得有关Reddit API here的OAUTH设置的更多详细信息