I am trying to call an api in the react js but no matter how i do it i always am getting error. I tried to call it using axios like this:
axios
.get("http://goldpricez.com/api/rates/currency/PKR/measure/ounce?X-API-KEY=API-KEY HERE")
.then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
})
.finally(function() {
// always executed
});
I also tried using fetch to do this:
fetch(
"http://goldpricez.com/api/rates/currency/PKR/measure/ounce?X-API-KEY=API-KEY-HERE")
.then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log("Looks like there was a problem: \n", error);
});
but both ways ended with the same error. After searching the stackoverflow i found a couple of same queries but in all cases they had the server side in control too. In my case I am using a third party library so i cannot change Access-Control-Allow-Origin even if i wanted to on the server side. For testig purposes i am using an extension of chrome that some how bypass this error and i get the response. Is there anyother thing i can do to make it work cause in production not all users might be using the chrome and even if they do its highly unlikely they will be using the cors extension.