【问题标题】:' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource' from origin 'http://localhost:3000' 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头
【发布时间】:2019-11-08 22:03:39
【问题描述】:

我已经尝试解决这个问题一个多星期了。我是这颗流星的新手。即使我不太懂英语,我也在努力自学。
但我正在尝试访问一个我得到这个 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 })
        })    
    }

【问题讨论】:

标签: reactjs


【解决方案1】:

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

【讨论】:

  • 谢谢,但我出现在控制台中,但我想要的是将其保存在变量中并将其放入 web no console {this.state.res.map (dat => { return ( {dat.codigo} {dat.nombre} {dat.carrera}
猜你喜欢
  • 2021-02-20
  • 2020-11-26
  • 2020-03-03
  • 2020-04-16
  • 1970-01-01
  • 2020-01-24
  • 2021-12-27
  • 2019-12-05
相关资源
最近更新 更多