xxx.js
onLoad: function () {
this.getTableData();
}, /*onLoad-end */
getTableData: function () {//自定义函数名称
var that = this;
// 这个地方非常重要,重置data{}里数据时候setData方法的this应为以及函数的this, 如果在下方的sucess直接写this就变成了wx.request()的this了
wx.request({
//请求接口的地址
url: 'http://api.yytianqi.com/forecast7d?city=39.93,116.40&key=bopi3li1ip93ae0n',
data: {},
header: {
"Content-Type": "applciation/json" //默认值
},
success: function (res) {
//res相当于ajax里面的返回的数据
console.log(res.data);
//如果在sucess直接写this就变成了wx.request()的this了
//必须为getTableData函数的this,不然无法重置调用函数
that.setData({
datas: res.data //datas传值给页面的,可以自定义命名
})
},
fail: function (err) { },//请求失败
complete: function () { }//请求完成后执行的函数
})
}