util.httpget的res写法不对吗?res无法反馈。
util.js页面
const httpget = function(tourl, data) {
// load提示框,可以自定义
console.log('tourl', data)
wx.showLoading({
title: "数据加载中",
mask: true,
});
wx.request({
url: tourl,
data: data,
method: 'POST', //接收请求的方式,如果不传默认为POST
header: {
'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
},
success: (res) => {
wx.hideLoading(); // 隐藏load框
return(res)
}
})
}
module.exports = {
showToast: showToast,
formatImgUrl: formatImgUrl,
formatTime: formatTime,
httpget: httpget
}
index.js页面
data: {
tourl: baseURL + '1.asp',
urldata: {
id1: 6,
id2: 'addoil',
id3: '16665',
},
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
this.setTabBar();
util.httpget(this.data.tourl, this.data.urldata).then(res => {
console.log(res)
});
// app.httpget({
// tourl: baseURL + '1.asp',
// });
},
异步请求不能return的 要用Promise 你可以参考我这个
const httpget = function(tourl, data) {
return new Promise((resolve, reject) => {
wx.showLoading({
title: "数据加载中",
mask: true,
});
wx.request({
url: tourl,
data: data,
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
},
timeout: 5000, // 设置请求超时时间为5秒
success: (res) => {
wx.hideLoading();
resolve(res); // 请求成功,调用 resolve 函数返回结果
},
fail: (err) => {
wx.hideLoading();
reject(err); // 请求失败,调用 reject 函数处理错误
},
complete: () => {
wx.hideLoading();
}
});
});
}
module.exports = {
showToast: showToast,
formatImgUrl: formatImgUrl,
formatTime: formatTime,
httpget: httpget
};