百度API免费接口获取天气预报-创新互联
#result{width: 800px;border:1px solid #ddd;margin-top: 30px;background: #aaa;display: none;}
#result p{height: 25px;line-height: 25px;padding-left: 20px;color: #fff;font-weight: bold;font-size: 16px;}
#result p span{padding: 0 10px;display: inline-block;width: 120px;text-align:center;height: 25px;line-height: 25px;}
#result p span.temp{width: 250px;text-align: left;margin-left: 20px;}
$(function(){
$(window).on('load',function(){
$.ajax({
'type':'get',
'url':'http://apis.baidu.com/heweather/weather/free?city=beijing',
'headers':{'apikey':'2c6d2927ef76200697018e989f6afbbb'},
'success':function(data){
$('#result').show();
data = data['HeWeather data service 3.0'][0];
var basic_info = '城市:' + data.basic.cnty + ' ' + data.basic.city;
var tianqi = data.daily_forecast;
$('
' + basic_info + '
').appendTo('#result');$('
日期气温天气
').appendTo('#result');$.each(tianqi,function(index,element){
var html = '';
var date = element.date;
var temperature = element.tmp.min + '°C - ' + element.tmp.max + '°C';
var w1 = element.cond.txt_d;
var w2 = element.cond.txt_n;
var weather;
if(w1 == w2){
weather = w1;
}
else{
weather = w1 + ' 转 ' + w2;
}
html += ''+ date +'' + ''+ temperature +'' + ''+ weather +'';
$('
'+ html +'
').appendTo('#result');});
}
});
});
$('#query').on('click',function(){
$('#result').hide().empty();
var city = $.trim($('#city').val());
$.ajax({
'type':'get',
'url':'http://apis.baidu.com/heweather/weather/free',
'data':{'city':city},
'headers':{'apikey':'2c6d2927ef76200697018e989f6afbbb'},
'success':function(data){
$('#city').val('');
data = data['HeWeather data service 3.0'][0];
console.log(data);
var basic_info = '城市:' + data.basic.cnty + ' ' + data.basic.city;
var tianqi = data.daily_forecast;
$('
' + basic_info + '
').appendTo('#result');$('
日期气温天气
').appendTo('#result');$.each(tianqi,function(index,element){
var html = '';
var date = element.date;
var temperature = element.tmp.min + '℃ - ' + element.tmp.max + '℃';
var w1 = element.cond.txt_d;
var w2 = element.cond.txt_n;
var weather;
if(w1 == w2){
weather = w1;
}
else{
weather = w1 + ' 转 ' + w2;
}
html += ''+ date +'' + ''+ temperature +'' + ''+ weather +'';
$('
'+ html +'
').appendTo('#result');$('#result').show();
});
}
});
});
});
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前题目:百度API免费接口获取天气预报-创新互联
文章URL:http://myzitong.com/article/dpcodo.html