近日,公司新开展一个项目用的react框架,其中有一个公共的部分是显示当天天气详情,当时就在网上看天气插件,有很多官方的天气插件,都是用<iframe></iframe>嵌套的。
在一个react框架又不想使用<iframe></iframe>,而且样式也不太好重写。所以,我尝试用新浪天气API接口获取天气,想要哪些数据就用那些数据,样式想怎么写就怎么写。
请看如下代码:返回的data值里即有我们想要的信息。
<script type="text/javascript">
var this_city;
$.ajax({
url: "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js",//可以获取ip得到城市名称
dataType: "script",
success: function(data){
if(remote_ip_info.ret == "1"){
this_city = remote_ip_info.city;
$.ajax({
type: "GET",
url: "http://wthrcdn.etouch.cn/weather_mini",
data: {
city: this_city
},
dataType:'json',
success:function(data){
/*console.log(data);*/
if(data.status == 1000){
var result = data.data.forecast[0];
$("#low").html(result.low.substring(3));
$("#high").html(result.high.substring(3));
$("#weatherType").html(result.type);
}else{
alert("获取数据错误");
};
}
});
}else {
alert('没有找到匹配的IP地址信息!');
};
}
});
</script>