7

I've tried to solve this problem for more than a week. I'm new to this meteor. I'm trying to learn by myself even because I do not know English very well.
but I'm trying to access an api that I get this eh encotrado that you should put a message like

Access-Control-Allow-Origin: *

but I do not know how and where

also try to put {mode: 'no-cors'}

fetch('http://sipla.cuci.udg.mx/sc/horariop.php?c=219359735&k=0d8ce4fab5f4df9ce711cae81e044e1a',{mode: 'no-cors'}) no work for me

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 })
        })    
    }
Til
  • 5,150
  • 13
  • 26
  • 34
  • 2
    Possible duplicate of [No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API](https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe) – Kate Orlova Jun 26 '19 at 22:49

1 Answers1

11

Assuming you're getting a CORS error when trying to hit that URL; you can add reverse-proxy CORS prefix to the URL to make your call to bypass it;

Just prepend 'https://cors-anywhere.herokuapp.com/' to the URL and you shouldn't get that cross origin error;

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));
Ilan P
  • 1,602
  • 1
  • 8
  • 13
  • thank you, but I show up in console, but what I wanted was to save it in variables and put it in web no console {this.state.res.map (dat => { return ( {dat.codigo} {dat.nombre} {dat.carrera} – Jose Martin Lopez Jun 27 '19 at 00:32
  • Not that using a middle-man was ever a great idea, that open proxy has been largely constrained now. It encourages you to self-host if you want to do this as a "real" fix. [More info here](https://github.com/Rob--W/cors-anywhere/issues/301). – ruffin Sep 05 '22 at 17:40