来源'http:// localhost:3000'中的'已被CORS策略阻止:所请求的资源上没有'Access-Control-Allow-Origin'标头

时间:2019-06-26 22:17:21

标签: reactjs

我已经尝试解决这个问题超过一个星期。我是新来的流星。即使我不太会英语,我也想自己学习。
但我正在尝试访问一个获得此eh encotrado的api,您应该输入

之类的消息
Access-Control-Allow-Origin: *

但我不知道如何在何处

还尝试放置{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 })
        })    
    }

1 个答案:

答案 0 :(得分:0)

假设您尝试访问该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));