在使用uniapp 开发过程中发现axios在小程序中不支持。处理办法如下:
方法1:自定义适配器
axios.defaults.adapter = function(config) { return new Promise((resolve, reject) => { var settle = require('axios/lib/core/settle'); var buildURL = require('axios/lib/helpers/buildURL'); uni.request({ method: config.method.toUpperCase(), url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer), header: config.headers, data: config.data, dataType: config.dataType, responseType: config.responseType, sslVerify: config.sslVerify, complete: function complete(response) { // console.log("执行完成:", response) response = { data: response.data, status: response.statusCode, errMsg: response.errMsg, header: response.header, config: config }; settle(resolve, reject, response); } }) })
方法2:使用axios-miniprogram-adapter适配器
import axios from ‘axios’
import mpAdapter from ‘axios-miniprogram-adapter’
axios.defaults.adapter = mpAdapter
注意,此方法axios需要0.26及一下版本。