axios封装

import axios from "axios";
import { message } from "antd";

// axios.defaults.baseURL = "http://localhost:3333";
axios.defaults.headers = { "Access-Control-Allow-Origin": "*" };

const Axios = axios.create({
  // baseURL: "",
  timeout: 20000,
  responseType: "json",
  withCredentials: true, // 是否允许带cookie这些
  headers: {
    "Content-Type": "application/json;charset=UTF-8",
  },
});

const request = {
  // Post  请求方式
  post(url, params = {}) {
    return Axios.post(url, params)
      .then((res) => {
        if (res.data.success) {
          return res.data;
        } else {
          message.warn(res.data.msg);
          return res.data;
        }
      })
      .catch((err) => {
        const messageErr = err && err.data && err.data.msg;
        message.error(messageErr || "接口异常");
      });
  },
  // GET 请求方式
  get(url, params = {}) {
    return Axios.get(url, { params })
      .then((res) => {
        if (res.data.success) {
          return res.data;
        } else {
          message.warn(res.data.msg);
          return res.data;
        }
      })
      .catch((err) => {
        const messageErr = err && err.data && err.data.msg;
        message.error(messageErr || "接口异常");
      });
  },
};

export default request;

 

axios封装

上一篇:Android 9.0 BufferSlot注解


下一篇:基于axios 封装的大文件分片上传