相关疑难解决方法(0)

使用axios在标头中传递JWT

我创建了一个带有节点后端的小项目,并通过使用Axios库的REST调用来反馈前端来获取数据.但是,当我用它传递标题时,我不断收到错误说Failed to load resource: the server responded with a status of 401 (Unauthorized).我发现了两种方法,但都没有用.

他们是

export const getUsersDetails=()=>{
  console.log('calling');
  return (dispatch) => {
    return axios.get('http://localhost:3030/users',{headers: { "Authorization": localStorage.getItem('jwtToken') }}).then((data)=>{
                 console.log('data comming',data);
                dispatch(getUsersData(data));
            }).catch((error)=>{
              console.log('error comming',error);
                dispatch(errorgetUsersData(error));
            });
        };
}
Run Code Online (Sandbox Code Playgroud)

axios.defaults.headers.common['Authorization'] = localStorage.getItem('jwtToken');
Run Code Online (Sandbox Code Playgroud)

但是当我使用邮递员时,我从后端获得所需的数据.我一直得到这个未经授权的错误的任何特殊原因?

http-headers axios

14
推荐指数
1
解决办法
2万
查看次数

标签 统计

axios ×1

http-headers ×1