axios 在body里带上token

首先安装axios

// axios
import global from './components/apicom/apicom'
Vue.prototype.api = global.apicom // 绑定原型链的公共api
import axios from 'axios';
Vue.prototype.$axios = axios;
axios.defaults.withCredentials = true; //让ajax携带cookie
或者
//axios请求配置及拦截
import axios from 'axios'
Vue.prototype.axios= axios;
axios.defaults.baseURL = 'xxxxxxxx';





axios.interceptors.request.use(function (config) {
  // console.log(localStorage.getItem('admin_info'))
  let token = ''
  if(localStorage.getItem('admin_info') == null){
    return config
  }else{
    token = JSON.parse(localStorage.getItem('admin_info')).token
  }
  
  console.log(token)
  if(config.data == '' || config.data == null || config.data ==undefined){
    if (token) {
      config.params['token'] = token
    }
    return config
    
  }else{
    if (token) {
      config.data['token'] = token
    }
    return config
  }

}, function (error) {

  return Promise.reject(error)
})

你可能感兴趣的:(vue,axios)