在main.js 中
Vue.prototype.ajax= function(param){
var _self = this,
url = param.url,
data = param.data || {},
header = param.header;
const token = uni.getStorageSync('token');
if(token){
header={
'token':token,
'content-type':'application/x-www-form-urlencoded'
}
}
//拼接完整请求地址
var requestUrl = this.$config.apiUrl + url;
//网络请求
uni.request({
url: requestUrl,
method: param.method,
header: header || {'content-type' : "application/json"},
data: data,
success: res => {
param.success(res.data)
},
fail: (e) => {
console.log("网络请求fail:" + JSON.stringify(e));
uni.showModal({
content:"" + res.errMsg
});
},
complete: () => {
//console.log("网络请求complete");
uni.hideLoading();
return;
}
})
}
其他页面引用
this.ajax({
method:'POST',
url:'/sys/mLogin',
data:{
username:this.userName,
password:this.password
},
success(res){
console.log(res)
}
})