js获取所在城市天气信息

1.引入jquery和cityjson

<script src="https://pv.sohu.com/cityjson?ie=utf-8" type="text/javascript"></script>

2.ajax请求

getWeather();
function getWeather() {
  jQuery.support.cors = true;
  $.ajax({
    url: "http://wthrcdn.etouch.cn/weather_mini",
    type: "GET",
    dataType: 'json',
    data: {city: (returnCitySN.cname).split('省')[1]}, //(returnCitySN.cname).split('省')[1]:获取所在的市
success:
function (res) { //var res=JSON.parse(res); var city = res.data.city;//所在城市 var maxTemperature = res.data.forecast[0].high;//最高温度 var minTemperature = res.data.forecast[0].low;//最低温度 var weather = minTemperature.split(' ')[1] + '~' + maxTemperature.split(' ')[1]; var type = res.data.forecast[0].type;//天气状态 }, error: function (err) { console.log(err) } }); }

 

cityjson
posted @ 2019-04-11 09:19  (⊙o⊙)买噶  阅读(7630)  评论(0编辑  收藏  举报