axios autohorization headers / CORS error

10,605

You've written Authorisation:

res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorisation');

But the header is Authorization as you have used with axios, try changing that and seeing if it works then,

Share:
10,605
Robertas Ankudovicius
Author by

Robertas Ankudovicius

Updated on June 17, 2022

Comments

  • Robertas Ankudovicius
    Robertas Ankudovicius almost 2 years

    I'm using react with axios to fetch some information. I'm sending JWT token to auth user and im geting response in console browser

    "Access to XMLHttpRequest at 'https://beer-tonight.herokuapp.com/beer/getBeerStatus' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response."

    This is my code:

    componentWillMount(){
    
        let token = localStorage.getItem('token');
    
    
        axios.get('https://beer-tonight.herokuapp.com/beer/getBeerStatus', {headers: {Authorization : 'Bearer ' + token}}).then(
        result => {
            console.log('yey');
        });
    }
    

    Error:

    enter image description here

    When I am using POSTMAN I am getting proper answer.

    Postman input:

    enter image description here

    p.s. i already added :

    app.use((req, res,next)=>{
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorisation');
    next();