' from origin 'http://localhost:3000' 已被 CORS 策略阻止:请求的资源上不存在 'Access-Control-Allow-Origin' 标头

Jos*_*pez 6 reactjs

我已经尝试解决这个问题一个多星期了。我是这个流星的新手。我正在努力自学,即使因为我不太懂英语。
但我正在尝试访问一个 api,我得到了这个 eh encotrado,你应该把这样的消息

Access-Control-Allow-Origin: *
Run Code Online (Sandbox Code Playgroud)

但我不知道如何以及在哪里

也试着把 {mode: 'no-cors'}

fetch('http://sipla.cuci.udg.mx/sc/horariop.php?c=219359735&k=0d8ce4fab5f4df9ce711cae81e044e1a',{mode: 'no-cors'}) 没有为我工作

componentDidMount() {

              fetch('http://sipla.cuci.udg.mx/sc/horariop.php?c=219359735&k=0d8ce4fab5f4df9ce711cae81e044e1a')

        .then((response) => {
          return response.json()
        })
        .then((dat) => { 
            this.setState( {datos1: dat })
        })    
    }
Run Code Online (Sandbox Code Playgroud)

小智 11

假设您在尝试访问该 URL 时遇到 CORS 错误;您可以在 URL 中添加反向代理 CORS 前缀以绕过它;

只需在 URL前面加上“ https://cors-anywhere.herokuapp.com/ ”,就不会出现跨源错误;

var url = 'https://cors-anywhere.herokuapp.com/http://sipla.cuci.udg.mx/sc/horariop.php?c=219359735&k=0d8ce4fab5f4df9ce711cae81e044e1a';
fetch(url, {
  method: 'GET',
  headers:{
    'X-Requested-With': 'XMLHttpRequest'
  }
}).then(res => res.json())
.then(response => console.log('Success:', response))
.catch(error => console.error('Error:', error));
Run Code Online (Sandbox Code Playgroud)