How to use the axios.interceptors function in axios

To help you get started, weโ€™ve selected a few axios examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tuwq / blog / blog_back / src / router / carry.js View on Github external
import { _set,_get,_remove } from 'base/js/cookie'
// ๆฏๆฌก่ฏทๆฑ‚ๆบๅธฆLOGIN_TOKEN
axios.interceptors.request.use(
    config => {
        if (_get('_TOKEN_')) {  // ๅˆคๆ–ญๆ˜ฏๅฆๅญ˜ๅœจtoken๏ผŒๅฆ‚ๆžœๅญ˜ๅœจ็š„่ฏ๏ผŒๅˆ™ๆฏไธชhttp header้ƒฝๅŠ ไธŠtoken
            config.headers._TOKEN_  = _get('_TOKEN_');
        }
        return config;
    },
    err => {
        return Promise.reject(err);
    }
);

// ๆฏๆฌกๆฃ€ๆŸฅTOKEN่ฟ‡ๆœŸ
axios.interceptors.response.use(
    response => {
    	if ( response.data.code == CONSTANT_CODE.TOKEN_MATURITY_LOGIN || response.data.code == CONSTANT_CODE.TOKEN_NOTUSER ) {
    		_remove('_TOKEN_')
            store.commit(types.SET_NOW_USER_INFO,undefined)
            router.replace({
              path: '/login',
              query: {redirect: router.currentRoute.fullPath}
            })
    	} else if (response.data.code == CONSTANT_CODE.TOKEN_MATURITY) {
            _remove('_TOKEN_')
            store.commit(types.SET_NOW_USER_INFO,undefined)
        }
        return response;
    },
    err => {
    	return Promise.reject(err);
github Zkliang1991 / Zkliang1991.github.io / 1901 / vue / hechenxi / src / utils / ajax.js View on Github external
//         text: 'ๅŠ ่ฝฝไธญ...',
    //         spinnerType: 'triple-bounce'
    //     });
    return config;
}, function (error) {
    // Do something with request error
    Toast({
        message: "ๆœช็Ÿฅ้”™่ฏฏ",
        duration: 500
    })
    return Promise.reject(error);
});


// // ๅ“ๅบ”ๆ‹ฆๆˆชๅ™จ response 
axios.interceptors.response.use(function (response) {
    console.log(response)
    // Indicator.close();
    Toast({
        message: response.data.msg,
        duration: 500
    });
    if (response.data.code == '401') {
        router.push({
            name: 'login'
        })
    }
    return response;
}, function (error) {
    Toast({
        message: "ๆœช็Ÿฅ้”™่ฏฏ",
        duration: 500
github LinWeb / blog / app / src / axios / index.js View on Github external
//     if (!checkLogin()) {
    //         window.location = '#login'
    //         return new Error('no login')
    //     }
    // }
    // config.withCredentials = true  // ่ฎพ็ฝฎๆต่งˆๅ™จ่‡ชๅŠจๅญ˜ๅ‚จๆœๅŠกๅ™จcookie
    let token = localStorage.getItem('token')
    if (token) {
        config.headers.Authorization = 'Bearer ' + token
    }
    return config
}, (error) => {
    return Promise.reject(error)
})
// ๆทปๅŠ ๅ“ๅบ”ๆ‹ฆๆˆชๅ™จ
axios.interceptors.response.use(function (response) {
    if (response.data.status !== 1) {
        message.error(response.data.message);
    }
    return response.data
}, function (error) {
    console.log(error)
    // Toast.fail(error.message, 1);
    return Promise.reject(error);
});


export default axios
github bestofjs / bestofjs-webui / src / helpers / loading.js View on Github external
init: function () {
    let self = this;
    // Add a request interceptor
    axios.interceptors.request.use(function (config) {
        // Do something before request is sent
        self.show();
        return config;
      }, function (error) {
        // Do something with request error
        return Promise.reject(error);
      }
    );
    // Add a response interceptor
    axios.interceptors.response.use(function (response) {
        // Do something with response data
        self.hide();
        return response;
      }, function (error) {
        // Do something with response error
        self.hide();
github wangy8961 / flask-vuejs-madblog / front-end / src / http.js View on Github external
// ๅŸบ็ก€้…็ฝฎ
if (process.env.NODE_ENV === 'production') {
  axios.defaults.baseURL = 'http://95.163.198.43:5000';
} else {
  axios.defaults.baseURL = 'http://127.0.0.1:5000';
}
// axios.defaults.baseURL = 'http://127.0.0.1:5000'
// axios.defaults.timeout = 5000  // ่ถ…ๆ—ถๆ—ถ้—ด๏ผˆๆฏซ็ง’๏ผ‰
// axios.defaults.retry = 2  // ้‡่ฏ•ๆฌกๆ•ฐ
// axios.defaults.retryDelay = 100  // ้‡่ฏ•ไน‹้—ด็š„้—ด้š”ๆ—ถ้—ด๏ผˆๆฏซ็ง’๏ผ‰

// ่ฏทๆฑ‚ๆ‹ฆๆˆชๅ™จ
// Add a request interceptor
axios.interceptors.request.use(function (config) {
  // Do something before request is sent
  const token = window.localStorage.getItem('madblog-token')
  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
}, function (error) {
  // Do something with request error
  return Promise.reject(error)
})

// ๅ“ๅบ”ๆ‹ฆๆˆชๅ™จ
// Add a response interceptor
axios.interceptors.response.use(function (response) {
  // Do something with response data
  return response
github geekape / geek-navigation / src / api / config.js View on Github external
import Storage from "../utils/localStorage"
const storage = new Storage('NAV')
// axiosๆ”น้€ 



// ่ฏทๆฑ‚ๅ‰
axios.interceptors.request.use(res => {
  return res
}, (error) => {
  return Promise.reject(error)
})

// ่ฏทๆฑ‚ๅŽ
axios.interceptors.response.use(response => {
  if (response.token) {
    storage.set('TOKEN', response.token);
  }
  if (response.status == 401) {
    router.push('/login');
  }

  return response
}, error => {
  router.push('/login');

  // ็œ็•ฅๅ…ถๅฎƒไปฃ็  ยทยทยทยทยทยท
  return Promise.reject(error)
})

function request(url = '', methods = 'get', params = {}) {
github LinWeb / blog / app / src / axios / index.js View on Github external
import axios from 'axios'
import { BASE_URL } from '@/config/api'
import { message } from 'antd'

axios.defaults.baseURL = BASE_URL;
axios.interceptors.request.use((config) => {
    // if (config.params) config.params.per_page_count = 4 // ๆต‹่ฏ•
    // Toast.loading('', 0)
    // if (PRIVATE_URLS.includes(config.url)) {
    //     // ๅˆคๆ–ญๅ“ชไบ›url้œ€่ฆ้ชŒ่ฏๆ˜ฏๅฆๅทฒ็™ปๅฝ•
    //     // ่ฆๆ€Žไนˆ่ทณ่ฝฌ๏ผŸ่ฆๆ€Žไนˆๅ–ๆถˆ่ฏทๆฑ‚๏ผŸ
    //     if (!checkLogin()) {
    //         window.location = '#login'
    //         return new Error('no login')
    //     }
    // }
    // config.withCredentials = true  // ่ฎพ็ฝฎๆต่งˆๅ™จ่‡ชๅŠจๅญ˜ๅ‚จๆœๅŠกๅ™จcookie
    let token = localStorage.getItem('token')
    if (token) {
        config.headers.Authorization = 'Bearer ' + token
    }
    return config
github mrin9 / Modular-Java-Jersey-Vue / web-ui / src / main.ts View on Github external
beforeCreate: function () {
    var me = this;

    //Load Language bundle (if not defined)
    if (this.$store.state.lang === undefined) {
      this.$store.commit('lang',"en");
    }
    loadLang(this.$store.state.lang);
    //Set the header Item
    if (this.$store.state.currentHeaderItem === undefined) {
      //this.$store.commit('currentHeaderItem',TopNav.items[0]);
    }

    //Set Axios Defaults and  global response Interceptor
    axios.defaults.timeout = RestUtil.ajaxTimeout;
    axios.interceptors.response.use(RestUtil.globalSuccessParser.bind(this), RestUtil.globalErrorParser.bind(this));

  },
  mounted(){
github vietnam-devs / coolstore-microservices / src / web / src / helper / interceptors.js View on Github external
export default function setup() {
  axios.interceptors.request.use(
    function(config) {
      const token = store.getters['account/accessToken']
      if (token) {
        config.headers.Authorization = `Bearer ${token}`
      }
      config.headers['Cache-Control'] = 'no-cache'
      return config
    },
    function(err) {
      return Promise.reject(err)
    }
  )

  axios.interceptors.response.use(
    function(response) {
      return response
github Kim09AI / react-music / server / utils / axios.js View on Github external
import axios from 'axios'

axios.defaults.baseURL = 'http://localhost:8080'

axios.interceptors.response.use(res => {
    return res && res.data
}, err => {
    return Promise.reject(err)
})

export default axios