I'm currently trying to implement an API for my company's web application, and I want to be able to make GET and POST requests to their endpoints to retrieve and add information. I tried it to make these requests first in Postman to make sure that our apikey worked properly, and the requests were successful. So I figured I would start out small and create a jsFiddle before actually implementing it inside our web application, but I ran into a little problem. When I tried running my jsFiddle code I ended up with the error "Access to XMLHttpRequest has been blocked by CORS policy"
I don't know much about axios and whether or not you can even use API keys within their requests, but an API key is required for me to retrieve any information from the different endpoints.
If anyone could help me understand where I'm going wrong that would be much appreciated, thanks.
My fiddle
const url = "https://someurl/api/ping"
const apiKey = "MY-KEY"
axios.get(url, {
headers: {'X-API-KEY': apiKey}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
I expected to get a response from this, but all I get is that error code. Works fine in Postman, but I'm using JSON as the raw body, so I'm not sure why axios won't work.