axios : Response to preflight request doesn't pass access control.

Created on 11 Mar 2016  ·  3Comments  ·  Source: axios/axios

I have an error when executing a get request in a ReactJs app does generate the following
"XMLHttpRequest cannot load http://localhost:3002/api/users. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3005' is therefore not allowed access. The response had HTTP status code 404."

My Code.

const ROOT_URL = 'http://localhost:3002';

const AUTH_TOKEN = 'eyJ0eXAiOiJKV1QiLCJhb..................';

axios.defaults.baseURL = ROOT_URL;
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8';

axios.get('/api/users').then(function(response) {
console.log(response.data);
console.log(response.status);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
});

Most helpful comment

This isn't an axios issue. You need to set your server to accept CORS requests. Look at the first answer to http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource

All 3 comments

This isn't an axios issue. You need to set your server to accept CORS requests. Look at the first answer to http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource

Hi,
I've already used it. by fixed at server added
"routes": {
"cors": true
}
at Hapi configuration connection
Thank you.

Hi @9tor have u resolve this problem?

Was this page helpful?
0 / 5 - 0 ratings