I am using axios to call api and bearer token way for authorization, my code:
export default axios.create({
baseURL,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${localStorage.getItem('token')}`
}
});
in UserRepository.js, the Repository is imported and the api be called by:
loadUsers() {
return Repository.get(`${users}`);
}
Then the error is "unauthorization", but when I update the header:
export default axios.create({
baseURL,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, PUT, POST, DELETE, OPTIONS',
'Content-Type': 'application/json',
'Authorization': `Bearer ${localStorage.getItem('token')}`
}
});
The error is: Access to XMLHttpRequest at 'http://example.com/users' from origin 'http://localhost:8080' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.