js獲取所在城市天氣信息

1.引入jquery和cityjsonjavascript

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

2.ajax請求java

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
相關文章
相關標籤/搜索