0

I am fetching API, but getting an Error: "Access to fetch at ${API} from origin 'http://localhost:3000' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled."

I've already tried setting up options, but it din't help.

componentDidMount() {
    navigator.geolocation.getCurrentPosition(position => {
      const apiKey = 'XXX';
      const lat = position.coords.latitude;
      const lon = position.coords.longitude;
      const options = {
        method: 'GET',
        headers: {
          'Content-type': 'application/json; charset=UTF-8'
        }
      };
      const url = `https://api.weatherbit.io/v2.0/current/airquality?lat=38.0&lon=-78.0&key=${apiKey}`;
      fetch(url, options).then(response => {
        if (response.status !== 200) {
          console.log(response.status);
        }
        response.json().then(data => {
          console.log(data);
        });
      });
    });
  }

I expect to get response looking like this:

  {  
     "lat":38,
     "lon":-78,
     "timezone":"America\/New_York",
     "city_name":"Louisa",
     "country_code":"US",
     "state_code":"VA",
     "data":[  
        {  
           "aqi": 12.345,
           "o3":57.3993,
           "so2":3.8594,
           "no2":5.42472,
           "co":383.437,
           "pm10":2.1667,
           "pm25":0.927972
        }
     ]
  }

But constantly continue getting this error.

  • To your `headers`, try adding `"Access-Control-Allow-Origin": "*"` – go_diego Jul 26 '19 at 17:05
  • Possible duplicate of [The 'Access-Control-Allow-Origin' header contains multiple values](https://stackoverflow.com/questions/22343384/the-access-control-allow-origin-header-contains-multiple-values) – Panther Jul 26 '19 at 17:07
  • @Panther That question is specific to asp.net mvc, and assumes the user has server access as well. In this case it is a 3rd party API, I don't think OP will get much help from the other question. – Anurag Srivastava Jul 26 '19 at 17:21
  • You will need to use a proxy to access the api – charlietfl Jul 26 '19 at 17:28

0 Answers0