首页 > 移动平台 > 详细

axios添加公共参数

时间:2020-06-28 11:55:21      阅读:354      评论:0      收藏:0      [点我收藏+]
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);
  })

 

axios添加公共参数

原文:https://www.cnblogs.com/zwhbk/p/13201738.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!