hbuilderx里创建的项目运行到微信开发者工具报错

微信开发者工具中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);
			}
		})
	})
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值