构建天气预告难度,web天气预先报告

亟待希图:jQuery Bootstrap 天气预告API(本文中采取API或者会失灵请灵活运用)

亟待常备不懈:jQuery Bootstrap 天气预告API(本文中使用API大概会失效请灵活运用)

WEB前端程序员(施行)制作天气预先报告难度:简单,web天气预先报告

亟需预备:jQuery Bootstrap 天气预告API(本文中使用API可能会失效请灵活运用)

CSS样式能够你自个儿去写那里只提议jQuery 请求数据和平化解析JSON数据 

{
 "resultcode":"200",
 "reason":"successed!",
 "result":{
  "sk":{
   "temp":"21",
   "wind_direction":"东北风",
   "wind_strength":"3级",
   "humidity":"66%",
   "time":"14:00"
  },
  "today":{
   "temperature":"17℃~22℃",
   "weather":"阴",
   "weather_id":{
    "fa":"02",
    "fb":"02"
   },
   "wind":"东北风3-4 级",
   "week":"星期三",
   "city":"无锡",
   "date_y":"2016年10月12日",
   "dressing_index":"较舒适",
   "dressing_advice":"建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。",
   "uv_index":"最弱",
   "comfort_index":"",
   "wash_index":"较适宜",
   "travel_index":"较适宜",
   "exercise_index":"较适宜",
   "drying_index":""
  },
  "future":{
   "day_20161012":{
    "temperature":"17℃~22℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期三",
    "date":"20161012"
   },
   "day_20161013":{
    "temperature":"17℃~21℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期四",
    "date":"20161013"
   },
   "day_20161014":{
    "temperature":"18℃~22℃",
    "weather":"阵雨",
    "weather_id":{
     "fa":"03",
     "fb":"03"
    },
    "wind":"东北风3-4 级",
    "week":"星期五",
    "date":"20161014"
   },
   "day_20161015":{
    "temperature":"17℃~22℃",
    "weather":"阵雨",
    "weather_id":{
     "fa":"03",
     "fb":"03"
    },
    "wind":"东北风3-4 级",
    "week":"星期六",
    "date":"20161015"
   },
   "day_20161016":{
    "temperature":"17℃~24℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期日",
    "date":"20161016"
   },
   "day_20161017":{
    "temperature":"17℃~21℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期一",
    "date":"20161017"
   },
   "day_20161018":{
    "temperature":"17℃~21℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期二",
    "date":"20161018"
   }
  }
 },
 "error_code":0
}

  

上边是要用到的JSON数据格式

搭建分界面略过 依照JSON中数据拉长到HTML中

 

分析数据到HTML中 个中鲜明要专注 跨域难题

$(document).ready(function() {
$.ajax({
type : “POST”,
dataType:’JSONP’,//跨域
jsonp:”callback”,
jsonpCallback:”fanyi”,
url:”
data :
{ip:”1二四.1二陆.230.180″,key:”b二a20八cb3九cec一c玖叁dd553496670828伍”},//请求数据
success : function(datas){//datas是重返的JSON数据

if (datas.resultcode == 200) {//依据再次回到的数据
决断是不是成功博得到JSON中的值
$(“#city”).text(datas.result.today.city);//解析数据到HTML文书档案中突显数据
$(“#date”).text(datas.result.sk.time+”发布”);
$(“#temp”).text(datas.result.sk.temp+”°”);
$(“#weather”).text(datas.result.today.weather);
构建天气预告难度,web天气预先报告。$(“#jt-tq”).text(datas.result.today.weather);
$(“#jt-wd”).text(datas.result.today.temperature);
$(“#jt-fx”).text(datas.result.today.wind);
$(“#two-week”).text(datas.result.future.day_20161013.week);
$(“#two-tq”).text(datas.result.future.day_20161013.weather);
$(“#two-wd”).text(datas.result.future.day_20161013.temperature);
$(“#two-fx”).text(datas.result.future.day_20161013.wind);
$(“#three-week”).text(datas.result.future.day_20161014.week);
$(“#three-tq”).text(datas.result.future.day_20161014.weather);
$(“#three-wd”).text(datas.result.future.day_20161014.temperature);
$(“#three-fx”).text(datas.result.future.day_20161014.wind);

}else{

//错误…

}

}
});
});

 

 

 

须要积谷防饥:jQuery Bootstrap 气候预告API(本文中选用API大概会失灵请灵活运用)
CSS样式可…

Ajax 通过城市名获取数据(全国天气预告API),ajaxapi

预览图(相比轻松粗糙)

澳门葡京 1

聚拢数据全国天气预先报告接口:

接口地址:
帮衬格式:json/xml
伸手情势:get
恳请示例:
调用样例及调试工具:API测试工具
恳请参数表明:
名称 类型 必填 说明
cityname string Y 城市名或城市ID,如:”德雷斯顿”,需求utf8 urlencode
dtype string N 再次来到数据格式:json或xml,暗许json
format int N 今后五日预先报告(future)二种回到格式,一或贰,暗中认可壹
key string Y 你报名的key

HTML部分代码:

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" >
<title>天气预报</title>
<script src="jquery-2.1.1.min.js"></script>
<link rel="stylesheet" href="w.css">
<div id="mf_weather">
<script src="w.js"></script>
<button id="search">天气查询</button>
<input id="city" type="text" value="tbody">
<div class="ctn">
<div id="mufeng">
</div>
<div id="future"></div>
</div> 
</html>

JavaScript部分:

/*
* 一.输入城市名
* 贰,点击的时候发送请求
* 三.响应成功渲染页面
* */
$(‘#search’).on(‘click’,function(){
var city = $(‘#city’).val()||’北京’;
$citycode=urlencode(city);
url=”;
$.ajax({url: url,
dataType: “jsonp”,
type:”get”,
data:{location:city},
success:function(data){
var sk = data.result.sk;
var today = data.result.today;
var futur = data.result.future;
var fut = “日期温度天气风向”;
$(‘#mufeng’).html(“<p>” + ‘当前: ‘ + sk.temp + ‘℃ , ‘ +
sk.wind_direction + sk.wind_strength + ‘ , ‘ + ‘空气湿度’ +
sk.humidity + ‘ , 更新时间’ + sk.time + “</p><p
style=’padding-bottom: 十px;’>” + today.city + ‘ 后天是: ‘ +
today.date_y + ‘ ‘ + today.week + ‘ , ‘ + today.temperature + ‘ , ‘ +
today.weather + ‘ , ‘ + today.wind + “<p></p>”);
$(‘#future’).html(“<p>” + ‘未来: ‘ + futur[0].temperature+ ‘℃ ,
‘ + futur[0].weather + futur[0].wind + ‘ , ‘ + ‘ , 更新时间’ +
futur[0].week+futur[0].date + “</p><p
style=’padding-bottom: 10px;’>” + today.city +
“<p></p>”);
} });
});
function urlencode (str) {
str = (str + ”).toString();
return encodeURIComponent(str).replace(/!/g, ‘%21’).replace(/’/g,
‘%27’).replace(/\(/g, ‘%28’).
replace(/\)/g, ‘%29’).replace(/\*/g, ‘%2A’).replace(/%20/g, ‘+’);
}
})

以上所述是小编给大家介绍的Ajax
通过城市名获取数据(全国天气预告API),希望对我们享有辅助,固然大家有其它疑问请给本身留言,小编会及时回复大家的。在此也非凡谢谢我们对帮客之家网址的支撑!

通过城市名获取数据(全国气候预告API),ajaxapi
预览图(相比较轻易粗糙)
聚合数据全国天气预告接口:
接口地…

CSS样式能够你本人去写那里只建议jQuery 请求数据和平消除析JSON数据 

CSS样式能够你本人去写那里只建议jQuery 请求数据和解析JSON数据 

{
 "resultcode":"200",
 "reason":"successed!",
 "result":{
  "sk":{
   "temp":"21",
   "wind_direction":"东北风",
   "wind_strength":"3级",
   "humidity":"66%",
   "time":"14:00"
  },
  "today":{
   "temperature":"17℃~22℃",
   "weather":"阴",
   "weather_id":{
    "fa":"02",
    "fb":"02"
   },
   "wind":"东北风3-4 级",
   "week":"星期三",
   "city":"无锡",
   "date_y":"2016年10月12日",
   "dressing_index":"较舒适",
   "dressing_advice":"建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。",
   "uv_index":"最弱",
   "comfort_index":"",
   "wash_index":"较适宜",
   "travel_index":"较适宜",
   "exercise_index":"较适宜",
   "drying_index":""
  },
  "future":{
   "day_20161012":{
    "temperature":"17℃~22℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期三",
    "date":"20161012"
   },
   "day_20161013":{
    "temperature":"17℃~21℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期四",
    "date":"20161013"
   },
   "day_20161014":{
    "temperature":"18℃~22℃",
    "weather":"阵雨",
    "weather_id":{
     "fa":"03",
     "fb":"03"
    },
    "wind":"东北风3-4 级",
    "week":"星期五",
    "date":"20161014"
   },
   "day_20161015":{
    "temperature":"17℃~22℃",
    "weather":"阵雨",
    "weather_id":{
     "fa":"03",
     "fb":"03"
    },
    "wind":"东北风3-4 级",
    "week":"星期六",
    "date":"20161015"
   },
   "day_20161016":{
    "temperature":"17℃~24℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期日",
    "date":"20161016"
   },
   "day_20161017":{
    "temperature":"17℃~21℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期一",
    "date":"20161017"
   },
   "day_20161018":{
    "temperature":"17℃~21℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期二",
    "date":"20161018"
   }
  }
 },
 "error_code":0
}
{
    "resultcode":"200",
    "reason":"successed!",
    "result":{
        "sk":{
            "temp":"21",
            "wind_direction":"东北风",
            "wind_strength":"3级",
            "humidity":"66%",
            "time":"14:00"
        },
        "today":{
            "temperature":"17℃~22℃",
            "weather":"阴",
            "weather_id":{
                "fa":"02",
                "fb":"02"
            },
            "wind":"东北风3-4 级",
            "week":"星期三",
            "city":"无锡",
            "date_y":"2016年10月12日",
            "dressing_index":"较舒适",
            "dressing_advice":"建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。",
            "uv_index":"最弱",
            "comfort_index":"",
            "wash_index":"较适宜",
            "travel_index":"较适宜",
            "exercise_index":"较适宜",
            "drying_index":""
        },
        "future":{
            "day_20161012":{
                "temperature":"17℃~22℃",
                "weather":"阴",
                "weather_id":{
                    "fa":"02",
                    "fb":"02"
                },
                "wind":"东北风3-4 级",
                "week":"星期三",
                "date":"20161012"
            },
            "day_20161013":{
                "temperature":"17℃~21℃",
                "weather":"阴",
                "weather_id":{
                    "fa":"02",
                    "fb":"02"
                },
                "wind":"东北风3-4 级",
                "week":"星期四",
                "date":"20161013"
            },
            "day_20161014":{
                "temperature":"18℃~22℃",
                "weather":"阵雨",
                "weather_id":{
                    "fa":"03",
                    "fb":"03"
                },
                "wind":"东北风3-4 级",
                "week":"星期五",
                "date":"20161014"
            },
            "day_20161015":{
                "temperature":"17℃~22℃",
                "weather":"阵雨",
                "weather_id":{
                    "fa":"03",
                    "fb":"03"
                },
                "wind":"东北风3-4 级",
                "week":"星期六",
                "date":"20161015"
            },
            "day_20161016":{
                "temperature":"17℃~24℃",
                "weather":"阴",
                "weather_id":{
                    "fa":"02",
                    "fb":"02"
                },
                "wind":"东北风3-4 级",
                "week":"星期日",
                "date":"20161016"
            },
            "day_20161017":{
                "temperature":"17℃~21℃",
                "weather":"阴",
                "weather_id":{
                    "fa":"02",
                    "fb":"02"
                },
                "wind":"东北风3-4 级",
                "week":"星期一",
                "date":"20161017"
            },
            "day_20161018":{
                "temperature":"17℃~21℃",
                "weather":"阴",
                "weather_id":{
                    "fa":"02",
                    "fb":"02"
                },
                "wind":"东北风3-4 级",
                "week":"星期二",
                "date":"20161018"
            }
        }
    },
    "error_code":0
}

  

  

地方是要用到的JSON数据格式

下边是要用到的JSON数据格式

搭建分界面略过 遵照JSON中数量拉长到HTML中

搭建分界面略过 依照JSON中数量增进到HTML中

 

 

分析数据到HTML中 当中必然要注意 跨域难题

解析数据到HTML中 在这之中自然要留心 跨域难点

$(document).ready(function() {
$.ajax({
type : “POST”,
dataType:’JSONP’,//跨域
jsonp:”callback”,
jsonpCallback:”fanyi”,
url:”
data :
{ip:”1二肆.1二陆.230.180″,key:”b二a208cb3玖cec一c玖三dd5534966708285″},//请求数据
success : function(datas){//datas是回到的JSON数据

$(document).ready(function() {
$.ajax({
type : “POST”,
澳门葡京 ,dataType:’JSONP’,//跨域
jsonp:”callback”,
jsonpCallback:”fanyi”,
url:”
data :
{ip:”1二肆.1二陆.230.180″,key:”b二a208cb3玖cec壹c九叁dd553496670828五”},//请求数据
success : function(datas){//datas是回去的JSON数据

if (datas.resultcode == 200) {//依据重回的数据
决断是不是成功博获得JSON中的值
$(“#city”).text(datas.result.today.city);//解析数据到HTML文书档案中展现数据
$(“#date”).text(datas.result.sk.time+”发布”);
$(“#temp”).text(datas.result.sk.temp+”°”);
$(“#weather”).text(datas.result.today.weather);
$(“#jt-tq”).text(datas.result.today.weather);
$(“#jt-wd”).text(datas.result.today.temperature);
$(“#jt-fx”).text(datas.result.today.wind);
$(“#two-week”).text(datas.result.future.day_20161013.week);
$(“#two-tq”).text(datas.result.future.day_20161013.weather);
$(“#two-wd”).text(datas.result.future.day_20161013.temperature);
$(“#two-fx”).text(datas.result.future.day_20161013.wind);
$(“#three-week”).text(datas.result.future.day_20161014.week);
$(“#three-tq”).text(datas.result.future.day_20161014.weather);
$(“#three-wd”).text(datas.result.future.day_20161014.temperature);
$(“#three-fx”).text(datas.result.future.day_20161014.wind);

if (datas.resultcode == 200) {//依据再次来到的数据
剖断是或不是成功收获到JSON中的值
$(“#city”).text(datas.result.today.city);//解析数据到HTML文书档案中显得数据
$(“#date”).text(datas.result.sk.time+”发布”);
$(“#temp”).text(datas.result.sk.temp+”°”);
$(“#weather”).text(datas.result.today.weather);
$(“#jt-tq”).text(datas.result.today.weather);
$(“#jt-wd”).text(datas.result.today.temperature);
$(“#jt-fx”).text(datas.result.today.wind);
$(“#two-week”).text(datas.result.future.day_20161013.week);
$(“#two-tq”).text(datas.result.future.day_20161013.weather);
$(“#two-wd”).text(datas.result.future.day_20161013.temperature);
$(“#two-fx”).text(datas.result.future.day_20161013.wind);
$(“#three-week”).text(datas.result.future.day_20161014.week);
$(“#three-tq”).text(datas.result.future.day_20161014.weather);
$(“#three-wd”).text(datas.result.future.day_20161014.temperature);
$(“#three-fx”).text(datas.result.future.day_20161014.wind);

}else{

}else{

//错误…

//错误…

}

}

}
});
});

}
});
});

 

 

 

 

 

 

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*
Website