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"


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM