微信小程序根据经纬度请求本地天气接口
onLoad: function (options) {
// 获取天气
wx.getLocation({ //获取经纬度
success: ({ latitude, longitude }) => {
wx.request({ //发起请求
url: 'http://www.test.com/wx/interface/setup/weather.php', //本地的天气接口地址
data: { //发送给后台的数据
x: latitude,
y: longitude
},
header: { //请求头
"Content-Type": "application/x-www-form-urlencoded"
},
method: "GET", //提交方式GET/POST
success: (res) => { //成功后返回值
console.log(res.data)
let weather = res.data.data //赋值给weather
console.log(weather)
this.setData( //发送到.wxml
{ weather }
)
},
})
}
})
}