I am trying to build a react app that calls a public API using axios.get(url)
to display match results from a game I play. I keep getting the error Access to XMLHttpRequest at 'url' from origin 'localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
which causes my GET request to fail.
I have tried looking at the Axios documentation and other stackoverflow posts on this issue but none of them seemed to have what I was looking for. Many of the answers were to allow CORS on the server-side but this is a public API so I don’t have access to the server. Is there a way to allow CORS just through the front-end react code using Axios to solve this problem? This react app is just for my own learning purposes and not for production so I do not need the best solution, I just want to be able to have my app work so that I can keep going on my project.
This is the code that I’m working with
axios.headers = {"Access-Control-Allow-Origin": "true"};
axios.get(url)
.then(response => {
//...code in here
});
Should I use something else instead of Axios that would be easier?