Skip to content Skip to sidebar Skip to footer

How To Solve 'access-control-allow-origin' Error When Trying To Access Api From Localhost

I've seen a lot of topics on this error, but I'm very new to js and apis, so I couldn't really understand much, so I apologize for noob status. I'm trying to access an api from spo

Solution 1:

If the backend support CORS, you probably need to add to your request this header:

headers: {"Access-Control-Allow-Origin": "*"}

Your code would be like this:

getPlayerInfo() {
  const apiKey = "my-api-key";
  const playerID = "41c44740-d0f6-44ab-8347-3b5d515e5ecf";
  const url = `http://api.sportradar.us/nfl/official/trial/v5/en/players/${playerID}/profile.json?api_key=${apiKey}`;
  const config = {
    headers: {'Access-Control-Allow-Origin': '*'}
  };


  axios.get(url,config).then(response =>console.log(response));
}

Hope this helps !

Post a Comment for "How To Solve 'access-control-allow-origin' Error When Trying To Access Api From Localhost"