uniapp使用axios(亲测可用)

添加一个axios-adapter.js

import axios from 'axios'

// 适配小程序网络请求
axios.defaults.adapter = function (config) {
  return new Promise((resolve, reject) => {
    var settle = require('axios/lib/core/settle');
    var buildURL = require('axios/lib/helpers/buildURL');
    const url = new RegExp('^http(s)?://').test(config.url) ? config.url : config.baseURL + config.url // 判断时相对路径还是绝对路径,相对路径添加config.baseURL
    uni.request({
      method: config.method.toUpperCase(),
      url: buildURL(url, config.params, config.paramsSerializer),
      header: config.headers,
      data: config.data,
      dataType: config.dataType,
      responseType: config.responseType,
      sslVerify: config.sslVerify,
      complete: function complete (response) {
        response = {
          data: response.data,
          status: response.statusCode,
          errMsg: response.errMsg,
          header: response.header,
          config: config
        };
        settle(resolve, reject, response);
      }
    })
  })
}

在main.js导入axiso-adapter.js

import "@/plugins/axios-dapter.js"
posted @ 2020-09-19 16:32  大圣巴巴  阅读(7794)  评论(0编辑  收藏  举报