通过jquery获取天气的方法

代码为:

$.getScript('http://int.dpool.sina.com/iplookup/iplookup.php?format=js',function(_result){

  if(remote_ip_info.ret=='1'){

     $.ajax({

         type:"GET",

         url:"http://wthrcdn.etouch.cn/weather_mini?city="+remote_ip_info.city,

         data:'',

         success:function(msg){

              console.log(msg);

           }

        });

   }

});

msg为一个字符串,里面包含了城市,5天的天气预报等信息。如果需要在页面进行显示,则在页面获取元素后,通过html()来进行设置内容。msg需要先通过eval()方法或者JSON.parse()方法来转换为JSON对象。然后对msg.data.*获取需要的属性值。如我设置了一个id为a的div,使里面显示天气内容:

var r=eval('('+msg+')');    //也可以使用JSON.parse(msg);
$('#a').html(r.data.city+"   "+r.data.forecast[0].date+r.data.forecast[0].fengli+r.data.forecast[0].high+r.data.forecast[0].low+r.data.forecast[0].type);
console.log(r);

原文地址:https://www.cnblogs.com/lionisnotkitty/p/6109471.html