js獲取當前城市,並根據當前城市獲得當地天氣

/**
 * 定位當前城市
 */
 function locationInit(){
  $.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js', function(_result) {
if (remote_ip_info.ret == '1') {
cityName = remote_ip_info.province;
// console.log(cityName)
$("#cityName").html(cityName);
  } 
 });

 }

/**
  * 根據城市獲得天氣
  */
  function cityWeatherInit(){
  //跨域是瀏覽器的安全策略.
  //jQuery 解決的方式.
  var relName = $("#cityName").html();
  var todayimg = '';
  var tomorrowimg = '';
  $.ajax({
  url:"http://api.map.baidu.com/telematics/v3/weather",
  type:"get",
  data:{
   location:relName,
   output:'json',
   ak:'祕鑰'
  },
  /*預期服務器端返回的數據類型,假設我現在跨域了,我就改成jsonp 就可以了 */
  dataType:"jsonp",
  success:function(data){
  var weatherData=data.results[0].weather_data;
  console.log(weatherData)               
  }
  }
  })
  }
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章