axios : TypeError: Cannot read property ‘FormData’ of undefined

使用axios 1.13在小程序下请求API,出现报错:

TypeError: Cannot read property 'FormData' of undefined
TypeError: Cannot read property ‘FormData’ of undefined

经过一番折腾,在此处找到答案:https://github.com/axios/axios/issues/5201

 

果断降至 0.27.2

降至 0.27.2 发现在小程序模式下依然报错:

TypeError: adapter is not a function
TypeError: adapter is not a function

因为是用的 uniapp ,在封装拦截器比如 request.js 中使用 uni.request 封装一个 adapter

axios.defaults.adapter = function(config) {
  return new Promise((resolve, reject) => {
      console.log(config)
      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) {
              response = {
                  data: response.data,
                  status: response.statusCode,
                  errMsg: response.errMsg,
                  header: response.header,
                  config: config
              };

              settle(resolve, reject, response);
          }
      })
  })
}

发表评论