微信小程序 request请求封装
var sendRequest = function (url, method, data = {}, header = {} )
{
var promise = new Promise(function (resolve, reject){
wx.request({
url: url,
data: data,
method: method,
header:header,
success: function(data) {
//做一些统一处理操作,例如401验证
//resolve用于具体调用中
resolve(data);
},
fail: function(data) {
reject(data);
}
})
})
return promise
}
//导入
module.exports = {
sendRequest : sendRequest
}
应用示例:
index.js页面
//加载util
const utils = require('../utils/util')
utils.sendRequest(your_request_url, 'GET', { user_id: user_id })
.then(function (response) {
that.setData({
likes: response.data.data
})
}, function (error) {
console.log(error);
})