zoukankan      html  css  js  c++  java
  • axios 封装

    来自:https://www.jianshu.com/p/68d81da4e1ad 侵删

    import axios from 'axios' import qs from 'qs' let baseurl = 'http://localhost:8081/api' function getRequest (apiPath, params, successCallback, errorCallback) { let url = baseurl + apiPath let req = axios.get(url, { params: params, paramsSerializer: params => { return qs.stringify(params, { indices: false }) }}) thenResponse(req, successCallback, errorCallback) } function postRequest (apiPath, params, successCallback, errorCallback) { let url = baseurl + apiPath let req = axios.post(url, qs.stringify(params, { indices: false })) thenResponse(req, successCallback, errorCallback) } function putRequest (apiPath, params, successCallback, errorCallback) { let url = baseurl + apiPath // var params = new URLSearchParams() // params.append('status', 0) let req = axios.put(url, qs.stringify(params, { indices: false })) thenResponse(req, successCallback, errorCallback) } function deleteRequest (apiPath, params, successCallback, errorCallback) { let url = baseurl + apiPath let req = axios.delete(url, { params: params, paramsSerializer: params => { return qs.stringify(params, { indices: false }) }}) thenResponse(req, successCallback, errorCallback) } /** * 请求结果处理 * @param axiosRequest * @param successCallback * @param errorCallback */ function thenResponse (axiosRequest, successCallback, errorCallback) { axiosRequest.then((res) => { if (res.status === 200) { res = res.data if (res.code === 0) { if (successCallback) successCallback(res) } else { if (errorCallback) errorCallback(res.code, res.msg) } } }).catch((error) => { if (errorCallback) errorCallback(-1, error.message) }) } export { getRequest, postRequest, putRequest, deleteRequest }

    调用

    //导入封装axios的js文件
    import {getRequest, deleteRequest} from 'utils/api'
    //发送delete请求,带数组参数 与 普通参数
    deleteRequest('/admin/user/del', {
            token: ‘SDFSDIFJSIODFKMSDOFM,SDOPFK’,
            ids: [1,2,3,4,5,6]
          }, function (res) {
            _this.$alert('删除成功')
          }, function (errcode, errmsg) {
            _this.$alert('code ' + errcode + ' ,meaasge ' + errmsg)
          })
    //发送GET请求
    getRequest('/admin/users', {
            status: 0,
            pageNum: this.currentPage,
            pageSize: count,
            token: '111'
          }, function (res) {
            _this.totalPage = res.data.total
          }, function (errcode, errmsg) {
            _this.$alert('code ' + errcode + ' ,meaasge ' + errmsg)
          })
  • 相关阅读:
    TestMap HashMap的常见操作
    三种方式遍历一颗二叉树
    jmeter 5.1.1版本 进行抓包的时候弹出输入密码
    浅谈Java中的AOP面向切面的变成和控制反转IOC
    rpc测试框架
    SpringBoot下,@WebFilter配置获取日志
    某些测试工具
    Google的三大马车
    关于Mock的一些网站
    用Jmeter做性能测试,之后报表展示
  • 原文地址:https://www.cnblogs.com/Byme/p/10083188.html
Copyright © 2011-2022 走看看