How does axios send token request parameters?

I got the token when I logged in and stored it in localStorage. I want to know how to request data on axios

console.log(localStorage.getItem("token")); // 


// 
 beforeCreate() {
    this.axios
      .post(`/api/services/app/role/GetRoles`, {
        permission: ""
      })
      .then(response => {
        console.log(response.data.result);
      })
      .catch(function(error) {
        console.log(error);
      });
  }
};

I would like to ask how to bring token to request data. Thank you for your answer

Jun.25,2021

token this kind of thing can be put in the interceptor.

axios.interceptors.request.use(config => {
    config.headers.permission = localStorage.getItem("token")
    return config
})

usually puts token in request header. There are about 4 kinds of axios setting header

.
  1. can be headers in the config parameter in the post method
  2. create a new axios instance, with headers
  3. modify the global default config of axios
  4. interceptor

1. token is generally set in the header of the request.
2. Generally speaking, you need to configure global axios interception.
https://codeshelper.com/a/1190000015294743


header carries
if both need token, then interceptor


set the global default value

axios.defaults.baseURL = 'https://api.example.com';
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;    // 
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

see the manual https://www.kancloud.cn/yunye.

for details.
Menu