微信开发者工具中TypeError: Cannot read property ‘FormData’ of undefined
最近想测试蓝牙功能,因为在h5下不能使用蓝牙,所以想跑到微信开发者工具里面调用测试,但是一直显示报错
找了很久发现是axios版本问题,尝试重新引入版本还是不行,会报错adapter is not a function:
大致就是说适配问题不兼容,因为我们这里是引用的axios插件,在兼容方面还需要做相应处理。
方案一:使用原生的请求方式 uni.request()
//直接请求会报错 adapter is not a function
// this.$axios.get("/books")
// .then(res => {
// console.log(res)
// })
// 解决方法一:使用原生请求uni.request()
uni.request({
url:'http://x.x.x.x:8443/api/books',
data:{},
method:'GET',
success: (res) => {
console.log(res.data)
this.books=res.data
},
fail: (err) => {
console.log(err)
}
})
方案二:自定义适配器来适配uniapp的语法
在main.js中
// 解决方法二:使用axios请求
// 自定义适配器来适配uniapp语法
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);
}
})
})
}