Axios实例

get 请求
axios.get(url, 
    {
        headers: {
            'Authorization': 'Bearer ' + token,
            "Cookie" : 'sessionId=' + sessionId + '; recId=' + recId,
            ...
        },
        params: {
            param1: string,
            param2: string
        },
        ...
    }
)
.then(res => fn)
.catch(e => fn)
Post 请求
axios.post(url, 
    {
        data: data,
        ...
    },
    {
        headers: {
            'Authorization': 'Bearer ' + token,
            "Cookie" : 'sessionId=' + sessionId + '; recId=' + recId,
            ...
        }
    }
)
.then(res => fn)
.catch(e => fn)
axios.request({config}) 配置项
export interface Axios RequestConfig {
    	url?: string
    	method?: string
    	baseURL?: string
    	transformRequest?: Axios Transformer I Axios Transformer[
    	transformResponse?: AxiosTrans former Axios Trans former[]
    	headers?: any i
    	params serializer?:(params: any)=> string
    	data?: any;
    	timeout?: number;
    	withCredentials?: boolean;
    	adapter?: AxiosAdapter;
    	auth?: AxiosBasicCredentials:
    	responseType?: string
    	xsrfCookieName?: string
    	xsrfHeaderName?: string;
    	onUploadProgress?: (progressEvent: any )= void;
    	onDownloadProgress?: (progressEvent: any)=> void;
    	maxContentLength?: number
    	validateStatus?: (status: number )= boolean;
    	maxRedirects?: number;
    	httpagent?:any
    	httpsagent?:anyi
    	proxy ?: Axios Proxy Config false
    	cancel Token?: CancelToken
    }
代码参考
// 封装
request(url, method, { params, data, headers = {}, isApi = true, ...others } = {}) {
    const conf = {
      ...config,
      ...this.cfg,
      ...others,
      url: formatUrl(url, isApi, this.apiPrefix),
      method,
      headers,
      // disable browser's cache
      // always make a new request
      params: {
        ...params,
        timestamp: new Date().getTime()
      },
      data
    };
    conf.headers = omitBy(conf.headers, v => typeof v === 'undefined');
    return this.$http.request(conf);
  }

  get(url, params, config = {}) {
    return this.request(url, 'GET', { ...config, params });
  }
  post(url, data, config = {}) {
    return this.request(url, 'POST', { ...config, data,
     headers: { 'X-RESOURCEID': getResourceId() } // 可单独设置
     });
  }
  // 引用
  getBillingGroup(cptCode,level) {
    return this.get('/hr/hrBillingGroup/get',{cptCode,level})
  }
  // 佣金规则 billingGroup
  RSbillingGroup(data) {
    return this.post('/hr/hrBillingGroup/billingGroup',data)
  }

axios 可通过Axios自带的qs.stringify()对参数进行序列化
序列化后Content-Type就变成了application/x-www-form-urlencoded
它声明了请求数据会以键值对(普通表单形式)的形式发送

//添加请求拦截器
axios.interceptors.request.use((config) => {
  //在发送请求之前做某件事
  if (config.method === 'post') {
    config.data = qs.stringify(config.data);
  }
  config.cancelToken = new axios.CancelToken(function (cancel) {
    store.commit('pushToken', { cancelToken: cancel })
  })
  return config;
}, (error) => {
  iview.Notice.warning({
    title: '参数错误'
  });
  return Promise.reject(error);
});

Git 参考地址
项目代码参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值