Great explanation on CORS No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API
I created a local asp.net mvc app using C# (httpclient) to POST to an API on a server that is set up to allow CORS and it works. But the javascript app I'm running locally returns the following:
Access to XMLHttpRequest at 'https://apiurl/auth/token' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
GET requests work in the js app. Here is a snippet of code:
axios({
method: 'post',
headers: {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
},
baseURL: 'https://apiurl',
url: '/oauth/token',
data: {
grant_type: 'password',
username: this.state.username,
password: this.state.password
}
}).then(resp => {
console.log(resp);
});
API is WEBAPI with CORS enabled.
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
I also understand proxying is a potential solution, but I am curious to know if I am missing something here.