发起请求,获取天气数据
.js
//获取应用实例
const app = getApp()
Page({
data:{
msg:"Home",
id:"txt",
index:"5",
yiyan:{},
weather:{},
flag:false,
imgurl: [{
url: "../../images/b3.jpg"
}, {
url: "../../images/b2.jpg"
}, {
url: "../../images/b1.jpg"
}, {
url: "../../images/b1.jpg"
}, {
url: "../../images/b2.jpg"
}, {
url: "../../images/b3.jpg"
}, {
url: "../../images/b1.jpg"
}],
flag:false,
obj:{
name:"惠普",
index:"0",
time:"18"
}
},
click:function(){
var _this = this;
//发起一个网络请求
// wx.request({
// url: 'https://v1.hitokoto.cn/', //仅为示例,并非真实的接口地址
// data: {
// c: "a"
// },
// header: {
// 'content-type': 'application/json' // 默认值
// },
// success(res) {
// // console.log(res.data)
// _this.setData({
// yiyan: res.data
// })
// }
// });
// //发起一个网络请求 weather
// wx.request({
// url: 'https://www.tianqiapi.com/api/', //仅为示例,并非真实的接口地址
// method:"get",
// dataType:"json",
// data: {
// version: "v1",
// city:'湛江'
// },
// header: {
// 'content-type': 'application/json' // 默认值
// },
// success(res) {
// console.log(res.data)
// _this.setData({
// weather:res.data,
// flag:true
// })
// // console.log(res.data);
// }
// })
// },
// onLoad: function () {
// var _this = this;
// wx.request({
// url: 'https://v1.hitokoto.cn/', //仅为示例,并非真实的接口地址
// method:"get",
// dataType:"json",
// //请求参数
// data: {
// c:"a",
// encode:"json"
// },
// header: {
// 'content-type': 'application/json' // 默认值
// },
// success(res) {
// _this.setData({
// yiyan: res.data
// })
// })
}
})
.wxml
<text>{{yiyan.hitokoto}}</text>
<text>{{yiyan.from}}</text>
<view wx:if="{{flag}}">
当前查询的城市{{weather.city}}
<text>天气:{{weather.data[0].wea}}</text>
<view></view>
<text>穿衣指数:{{weather.data[0].index[3].desc
}}</text>
</view>
<button bindtap='click'>点击</button>
{{weather.data[1].index[2].title}}