axios添加公共参数


import qs from 'qs';
import axios from 'axios';

// 请求的拦截器
axios.interceptors.request.use(function (config) {
    const token = localStorage.getItem('token')
    const uid = localStorage.getItem('uid')
     // 判断请求的类型
     // 如果是post请求就把默认参数拼到data里面
     // 如果是get请求就拼到params里面
    if(config.method === 'post') {
        let data = qs.parse(config.data)

        config.data = qs.stringify({
            token: token,
            uid: uid,
            ...data
        })
    } else if(config.method === 'get') {
        config.params = {
            token: token,
            uid: uid,
            ...config.params
        }
    }
    return config;
  }, function (error) {
    return Promise.reject(error);
  })

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM