zoukankan      html  css  js  c++  java
  • 2-vue中Axios封装请求

    import axios from 'axios'
    import {Message} from 'element-ui'
    axios.interceptors.request.use(config => {
      return config;
    }, err => {
      Message.error({message: '请求超时!'});
      // return Promise.resolve(err);
    })
    axios.interceptors.response.use(data => {
      if (data.status && data.status == 200 && data.data.status == 500) {
        Message.error({message: data.data.msg});
        return;
      }
      if (data.data.msg) {
        Message.success({message: data.data.msg});
      }
      return data;
    }, err => {
      if (err.response.status == 504 || err.response.status == 404) {
        Message.error({message: '服务器被吃了⊙﹏⊙∥'});
      } else if (err.response.status == 403) {
        Message.error({message: '权限不足,请联系管理员!'});
      } else if (err.response.status == 401) {
        Message.error({message: err.response.data.msg});
      } else {
        if (err.response.data.msg) {
          Message.error({message: err.response.data.msg});
        }else{
          Message.error({message: '未知错误!'});
        }
      }
      // return Promise.resolve(err);
    })
    let base = '';
    export const postRequest = (url, params) => {
      return axios({
        method: 'post',
        url: `${base}${url}`,
        data: params,
        transformRequest: [function (data) {
          let ret = ''
          for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
          }
          return ret
        }],
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      });
    }
    export const uploadFileRequest = (url, params) => {
      return axios({
        method: 'post',
        url: `${base}${url}`,
        data: params,
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      });
    }
    export const putRequest = (url, params) => {
      return axios({
        method: 'put',
        url: `${base}${url}`,
        data: params,
        transformRequest: [function (data) {
          let ret = ''
          for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
          }
          return ret
        }],
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      });
    }
    export const deleteRequest = (url) => {
      return axios({
        method: 'delete',
        url: `${base}${url}`
      });
    }
    export const getRequest = (url) => {
      return axios({
        method: 'get',
        url: `${base}${url}`
      });
    }

    使用请求的封装

  • 相关阅读:
    编程思想
    为什么静态成员、静态方法中不能用this和super关键字
    C#中静态与非静态方法比较
    数组,集合,列表的使用与区别
    2017-3-23 网络IP
    [精彩] 关于DB2的内存分配
    DB2 常用命令
    SQL0973N在 "<堆名>" 堆中没有足够的存储器可用来处理语句
    DB2通用数据库性能调整的常用方法
    创建DB2数据库联合对象
  • 原文地址:https://www.cnblogs.com/gfbzs/p/12336927.html
Copyright © 2011-2022 走看看