Apprentice Apprentice - 5 days ago 3
Javascript Question

No 'Access-Control-Allow-Origin' header is present on requested resource

I know this qquestion has asked several times but none has solved my problem.I am using axios for fetching the token from the server(http://dev.site.com:82/getToken). I am working on localhost. I have supplied the header too but still i get an error of

No 'Access-Control-Allow-Origin' header is present
. Is this error being produced from my js code(axios) or it is a problem in server?

const config = {
method: 'get',
url: `${API_URL}/getToken`,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
},
};

export function getToken() {
return function (dispatch) {
console.log('action triggered atleast');
axios.request(config)
.then(response => {
console.log('response', response);
localStorage.setItem('token', response.data.token);
dispatch({ type: GET_TOKEN });
})
.catch(error => {
errorHandler(dispatch, error.response, TOKEN_ERROR);
});
};
}


Have i done any mistake or missed something in my code?

Answer

The server need to send the back the CORS header not the request

Take a look here how to enable cors: http://enable-cors.org/server.html

If you don't have access to the server then maybe you want to try something like a cors proxy