axios拦截code码,错误处理。vue2版本

直接上代码

import axios from 'axios'

import {Notification } from 'element-ui';

const request = axios.create({
  timeout: 10000
})

// response  拦截器 获取后台数据前拦截
request.interceptors.response.use(
  response => {
    const code = response.data.code || 200 ;//若未设置默认成功状态
    if (code !== 0 && code !== 200) {
      Notification({
        title: '提示',
        message: response.data.message,
        type: 'error',
        duration:'2000'
      });
      return response.data
    } else {
      return response.data
    }


  },
  error => {
    console.log('axios中response报错', error);
    Notification({
      title: '提示',
      message: '服务暂不可用',
      type: 'error',
      duration:'2000'
    });

    return Promise.reject(error);
  }
)

export default request

posted @ 2022-11-29 15:42  风意不止  阅读(372)  评论(0编辑  收藏  举报