import axios from "axios";//引入axios import { message } from "antd";//引入antd全局message export default function ajax(url, data = {}, type = 'GET', headers) { return new Promise((resolve, reject) => { let promise; if (type === 'GET'){ promise = axios.get(url, { params: data, headers }) }else if(type === 'POST'){ promise = axios.post(url, data,{headers}) }else if (type === 'PUT') { promise = axios.put(url, data,{headers}) }else if (type === 'DELETE') { promise = axios.delete(url,{ params : data, headers }) } promise.then(response => { resolve(response.data) }).catch(error => { message.error('请求出错了:' + error.message).then(r => {}) }) }) }
封装axios(promise风格)
最新推荐文章于 2023-08-12 09:52:53 发布
该代码段定义了一个函数,用于封装axios库的HTTP请求操作,支持GET、POST、PUT和DELETE方法。函数接受URL、数据、请求类型和headers作为参数,返回一个Promise,成功时解析为响应数据,失败时显示antd的message错误提示。
摘要由CSDN通过智能技术生成