码迷,mamicode.com
首页 > 其他好文 > 详细

调用第三方天气接口实现天气查询和展示

时间:2017-04-13 12:53:47      阅读:577      评论:0      收藏:0      [点我收藏+]

标签:with   response   ack   sda   rtm   request   res   creat   cti   

//跨域请求天气地址
			var crossDaomainRequest = {
				createCORS: function(method, url) {
					var xhr = new XMLHttpRequest();
					if(‘withCredentials‘ in xhr) {
						xhr.open(method, url, true);
					} else if(typeof XDomainRequest != ‘undefined‘) {
						var xhr = new XDomainRequest();
						xhr.open(method, url);
					} else {
						xhr = null;
					}
					return xhr;
				},
				get: function(url, callback) {
					var request = this.createCORS(‘get‘, url);
					if(request) {
						request.onload = function() {
							callback(request.response? request.response: request.responseText)
						};
						request.send();
					}
				}
			};

			crossDaomainRequest.get(‘http://wthrcdn.etouch.cn/WeatherApi?citykey=101100301‘, function(result) {				
				 var elements = $(result);	
				  if (elements.length > 0) {
		            var cityName = elements.find("city").text();  //当前城市
		            var updateTtime = elements.find("updatetime").text(); //更新时间 
		            var wendu = elements.find("wendu").text();  //温度
		            var shidu = elements.find("shidu").text();  //湿度
		            var fengxiang = elements.find("fengxiang").first().text(); //风向
		            var fengli = elements.find("fengli").first().text();  //风力
		            var weatherToday = elements.find("weather").first(); //天气详情包含白天和夜晚
		            var date = weatherToday.find("date").text(); //天气详情包含白天和夜晚
		            var high = weatherToday.find("high").text().replace("高温", "").replace("℃","").replace(" ","");
		            var low = weatherToday.find("low").text().replace("低温", "").replace("℃", "").replace(" ", "");
		            //alert(high+‘~‘+low)
		            var dayType = weatherToday.find("day").find("type").text();
		            var dayfengxiang = weatherToday.find("day").find("fengxiang").text();
		            var dayfengli = weatherToday.find("day").find("fengli").text();

		            var nightType = weatherToday.find("night").find("type").text();
		            var nightfengxiang = weatherToday.find("night").find("fengxiang").text();
		            var nightfengli = weatherToday.find("night").find("fengli").text();
		            var dImageUrl = ("/Scripts/Bus/DynamicDiagnosis/images/weather/" + dayType + ".gif");  //白天天气的图片 
		            var nImageUrl = ("/Scripts/Bus/DynamicDiagnosis/images/weather/n" + nightType + ".gif");
		            var strTmplate = "<div class=\"weather-icon\">" +
		                "<span class=\"day-icon\" style=\"background-image:url(" + dImageUrl + "); background-size: 100% 100%; \" title=\"白天:" + dayType + " " + dayfengxiang + " " + dayfengli + "\" ></span>" +
		                "<span class=\"night-icon\" style=\"background-image:url(" + nImageUrl + "); background-size: 100% 100%; \" title=\"夜间:" + nightType + " " + nightfengxiang + " " + nightfengli + "\"></span>" +
		                "</div>" +
		                "<div class=\"w-left\">" +
		                "<h2>今日天气<span>" + cityName + "</span></h2>" +
		                "<dl class=\"temperature clearfix\">" +
		                " <dt >" + low + "~" + high + "</dt>" +
		                    "<dd>" + "℃ " + fengxiang + fengli+
		                        "<div>白天:" + dayType + " " + dayfengxiang + " " + dayfengli + "</div>" +                          
		                       " </dd>" +
		                " </dl>" +
		                "<div class=\"realtime-tem\" style=\"font-size:12px;\">" + (new Date().getMonth() + 1) + "月" + date + "(实时:" + wendu + "℃,湿度:" + shidu + ")</div>" +
		            "</div>";
		            $(document.body).html(strTmplate);
				}
				
		 
				
		 });

 

调用第三方天气接口实现天气查询和展示

标签:with   response   ack   sda   rtm   request   res   creat   cti   

原文地址:http://www.cnblogs.com/hzy168/p/6703232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!