//包装好的 get 和 post 请求 common包
module.exports = {
get(apiUrl, yes, error) {
my.httpRequest({
url: apiUrl,
header: { 'Content-Type': 'application/json' },
method: 'GET',
success: yes,
fail: error
})
},
post(apiUrl, params, yes, error) {
my.httpRequest({
url: apiUrl,
data: params,
header: { 'Content-Type': 'application/x-www-form-urlencoded' },
method: 'POST',
success: yes,
fail: error
})
}
}
var that=this 一定要!!!
//界面使用
onLoad() {
//!!!this 指向修改
var that=this
//加载时返回用户信息
// contentReq.reportsList()
const http = require('../../common/com')
http.get('http://你自己的apiurl/et_access_token',function(res){
if(res.data){
console.log(res.data)
that.setData({
data:res.data
//你要赋值的data
})
}
},function(err){
console.log('error'+JSON.stringify(err))
})
},
ps:今天又是和bug说byebye的一天!
本文介绍了一个用于包装GET和POST请求的Common包,重点讲解了如何正确处理`this`指向,并在实际场景中展示了如何在界面上使用封装的http请求函数获取用户信息。
984

被折叠的 条评论
为什么被折叠?



