js代码
const ApiUrl = '';
const ajax = (opt) => {
uni.showLoading({
title: '加载中',
mask:true
});
opt = opt || {};
opt.url = opt.url || '';
opt.data = opt.data || null;
opt.method = opt.method || 'POST';
opt.header = opt.header || {
// "Content-Type": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": uni.getStorageSync('access_token')
};
opt.success = opt.success || function() {
};
uni.request({
url: ApiUrl + opt.url,
data: opt.data,
method: opt.method,
header: opt.header,
dataType: 'json',
success: function(res) {
uni.hideLoading()
opt.success(res);
},
fail: function(res) {
console.log(res)
uni.showToast({
title: '请稍后重试'
});
}
})
}
export {
ajax
}
在页面引用一下
import {
ajax
} from "js文件路径"
用法
ajax({
url: 'url',
data: {},
method: 'GET',
success: res => {
}
});
有问题联系 1612803019