百度地图的api可以查询天气,但是需要是服务端才可以使用,作为前端,没有后端的辅助想要实现的话 就需要借助node和koa2来实现啦
api
smsApi.post('/weather', async ctx => {
console.log(ctx.request.body)
const code = ctx.request.body.code;
var data2 = await getWeather(code);
// console.log(await getWeather(code))
// console.log(data)
ctx.json(data2);
});
// 查询天气
async function getWeather(code) {
var data2 = null;
var data = JSON.stringify({"title": "title", "tag": "tag", "content": "content", "photos": ["1", "2"]});
var config = {
method: 'get',
url: 'https://api.map.baidu.com/weather/v1/?district_id=' + code + '&data_type=all&ak=ak',
headers: {
'Content-Type': 'application/json',
},
data: data
};
await axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
// return response
data2 = response.data
})
.catch(function (error) {
console.log(error);
data2 = error
return error
});
return data2
}