使用api獲取數據————小程序
onLoad: function (options) { //打開頁面即執行。
let that = this;
wx.request({ //建立鏈接
url: 'http://web.juhe.cn:8080/constellation/getAll', //api獲取的地址
data: {
consName: "巨蟹座", //給api傳輸的數據
type: "today", //給api傳輸的數據
key: app.globalData.constKey, //apiKey,沒鑰匙人家就不會給你數據了。
},
header: {
'content-type': 'application/json' //請求頭,沒頭這哪成啊!!!
},
success(res) { //請求成功!!
console.log(res.data) //請求成功,就讓它告訴我一些唄,順便看看數據有些啥,後面好操作。
that.setData({
number: res.data.number, //獲取想要的數據,和定義那些數據。
all: res.data.all, //這些數據定義完就可以直接使用了。
})
}
})
},