分析原因:
uniapp的axios不适配
解决办法:
用uni封装一个request请求
一.新建config.js文件
export const VUE_APP_API_URL = '测试地址';
二.新建utils/http.js文件
import {
VUE_APP_API_URL
} from '../config/index.js'
let needLoadingRequestCount = 0;
let loadingTimer;
function showLoading(title = '', mask = true) {
if (needLoadingRequestCount === 0) {
uni.showLoading({
title,
mask
});
loadingTimer = setTimeout(() => {
if (needLoadingRequestCount > 0) {
uni.hideLoading();
}
}, 10000);
}
needLoadingRequestCount++;
}
function hideLoading() {
if (needLoadingRequestCount <= 0) return;
needLoadingRequestCount--;
if (needLoadingRequestCount === 0) {
loadingTimer && clearTimeout(loadingTimer);
uni.hideLoading();
}
}
export const apiResquest = (urls,data) => {
let headerData = {
'token': uni.getStorageSync('token'),
'content-type': 'application/json'
}
let dataObj = null
dataObj = data
return new Promise((resolve, reject) => {
let url = VUE_APP_API_URL + urls;
showLoading('加载中', true)
hideLoading()
return uni.request({
url: url,
data: dataObj,
method: 'POST',
header: headerData,
success: (res) => {
if (res.data.status !== 200) {
uni.showToast({
title: '获取数据失败:' + res.data.msg,
duration: 1000,
icon: "none"
})
return;
}
resolve(res.data);
},
fail: (err) => {
reject(err);
console.log(err)
},
complete: () => {
}
});
})
}
三.新建api管理文件api/index.js
import { apiResquest } from '../utils/http.js'
export const getLogin = (data) => apiResquest('user/login',data)
四.页面调用api
import {getLogin } from '../../api/index.js'
getUserInfo({}).then((res)=>{
if(res.status == 200){
}
})