Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource

Tags:

reactjs

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 })
        })    
    }
like image 385
Jose Martin Lopez Avatar asked Jun 26 '19 22:06

Jose Martin Lopez


People also ask

How do I fix CORS policy no Access-Control allow origin?

To allow any site to make CORS requests without using the * wildcard (for example, to enable credentials), your server must read the value of the request's Origin header and use that value to set Access-Control-Allow-Origin , and must also set a Vary: Origin header to indicate that some headers are being set ...

How do I enable CORS on localhost?

1. Use the proxy setting in Create React App. "proxy": "https://cat-fact.herokuapp.com/", Now when you make an API request to https://localhost:3000/api/facts Create React App will proxy the API request to https://cat-fact.herokuapp.com/facts and the CORS error will be resolved.

How do I fix blocked by CORS policy?

Use a Chrome extension to add Access-Control-Allow-Origin header into every response. To find one of them, just head over to Chrome Webstore and type in "CORS", dozens will show up in the search result. Or you can install CORS Helper, CORS Unblock or dyna CORS right away.


1 Answers

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));
like image 128
Ilan P Avatar answered Nov 15 '22 11:11

Ilan P