配合AJAX天气预报的webService 之asp
网络编程 2021-07-05 10:36www.168986.cn编程入门
因为发现AJAX目前跨域有点问题,只能在IE里面设置才可以使用,所以自己就写了个简单ASP的webService,其实作用就是一个小偷程序,把雅虎的天气抓了过来,然后生成XML。先从这里开始吧,以后会考虑添加数据库,这样客户端写好后读取速度就很快了。
习惯了用JS写东西,一不小心ASP也全拿JS写的,呵呵,有时间再改成VB。
我再鄙视他一下,FLASH是N年前就搞过的,时间虽然丢的有点长了,在AW的帮助,慢慢就会有所起色。
总算平息了火,还算是又默默改变了一件事情。
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
<%
Session.CodePage = "65001"
Response.contentType="text/html"
Response.Expires = 0
var N=Request("n")
var Num=Request("id")
var url="http://xml.weather.yahoo./forecastrss?p="+N+"&u=c&id="+Num;
var xmldoc=Server.CreateObject("Microsoft.Xmlhttp");
xmldoc.open('GET',url,false);
xmldoc.send(null);
var response = xmldoc.responseXML.documentElement;
var lastNode=response.selectNodes("//rss/channel/lastBuildDate");
var cityNode=response.selectNodes("//rss/channel/yweather:location");
var windNode=response.selectNodes("//rss/channel/yweather:wind");
var atmosphereNode=response.selectNodes("//rss/channel/yweather:atmosphere");
var astronomyNode=response.selectNodes("//rss/channel/yweather:astronomy");
var conditionNode=response.selectNodes("//rss/channel/item/yweather:condition");
var forecastNode=response.selectNodes("//rss/channel/item/yweather:forecast");
var lastV=lastNode(0).firstChild.nodeValue
//编辑属性
function attri(a,b,c){
return a(b).attributes.getNamedItem(c).value
}
var cityV=attri(cityNode,0,"city")
var chillV=attri(windNode,0,"chill")
var directionV=attri(windNode,0,"direction")
var speedV=attri(windNode,0,"speed")
var humidityV=attri(atmosphereNode,0,"humidity")
var visibilityV=attri(atmosphereNode,0,"visibility")
var sunriseV=attri(astronomyNode,0,"sunrise")
var sunsetV=attri(astronomyNode,0,"sunset")
var textV=attri(conditionNode,0,"text")
var codeV=attri(conditionNode,0,"code")
var tempV=attri(conditionNode,0,"temp")
//转换英文为中文
var cityCH=EN2CH(cityV)
function EN2CH(obj){
if(obj=="Wuhan"){return "武汉"}
else if(obj=="Nanchang"){return "南昌"}
else if(obj=="Haikou"){return "海口"}
else if(obj=="Beijing"){return "北京"}
else if(obj=="Shanghai"){return "上海"}
else if(obj=="Guangzhou"){return "广州"}
else if(obj=="Yinchuan"){return "银川"}
else if(obj=="Zhengzhou"){return "郑州"}
else if(obj=="Changsha"){return "长沙"}
else if(obj=="Hanzhong"){return "杭州"}
else if(obj=="Hong Kong"){return "香港"}
else if(obj=="Xin'an"){return "西安"}
else if(obj=="Chengdu"){return "成都"}
else if(obj=="Qingdao"){return "青岛"}
else if(obj=="Guiyang"){return "贵阳"}
else if(obj=="Jinan"){return "济南"}
else if(obj=="Hefei"){return "合肥"}
else if(obj=="Fuzhou"){return "福州"}
else if(obj=="Nanjing"){return "南京"}
else{return obj}
}
//风向函数
var windCH=wind(directionV)
function wind(a){
if(a>337 && a<=360){return "北风"};
else if(a>=0 && a<=22){return "北风"};
else if(a>22 && a<=67){return "东北风"};
else if(a>67 && a<=112){return "东风"};
else if(a>112 && a<=157){return "东南风"};
else if(a>157 && a<=202){return "南风"};
else if(a>202 && a<=247){return "西南风"};
else if(a>247 && a<=292){return "西风"};
else if(a>292 && a<=337){return "西北风"};
}
//能见度函数
visibility=vis(visibilityV)
function vis(a){
return a/100
}
//转换天气为中文
var textCH=tEN2CH(textV)
function tEN2CH(obj){
if(obj=="Clear"){return "晴朗"}
else if(obj=="Sunny"){return "晴天"}
else if(obj=="Partly Cloudy"){return "局部有云"}
else if(obj=="Mostly Clear"){return "晴"}
else if(obj=="Mostly Sunny"){return "小云"}
else if(obj=="Mostly Cloudy"){return "多云"}
else if(obj=="Cloudy"){return "云"}
else if(obj=="Sunny/Wind"){return "晴转云"}
else if(obj=="AM Clouds/PM Sun/Wind"){return "云转晴"}
else if(obj=="AM Showers"){return "晨雨"}
else{return obj}
}
//天气图片
var weaNum=weaimg(codeV)
function weaimg(obj){
return "<img src=\"http\://us\.i1\.yimg\./us\.yimg./i/us/we/52/"+obj+"\.gif\" />"
}
//星期替换
function dayEn(obj){
if(obj=="Sun"){return "星期日"}
else if(obj=="Mon"){return "星期一"}
else if(obj=="Tue"){return "星期二"}
else if(obj=="Wed"){return "星期三"}
else if(obj=="Thu"){return "星期四"}
else if(obj=="Fri"){return "星期五"}
else if(obj=="Sat"){return "星期六"}
else{return obj}
}
%>
<table width="163" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td rowspan="6" align="center"><%=cityCH%><br />
<%=weaNum%><br />
<%=textCH%></td>
<td align="right">感觉</td>
<td align="center"><%=chillV%>°</td>
</tr>
<tr style="background-color:#E6F5FF">
<td align="right">实际</td>
<td align="center"><%=tempV%>°</td>
</tr>
<tr>
<td align="right">风向</td>
<td align="center"><%=windCH%></td>
</tr>
<tr style="background-color:#E6F5FF">
<td align="right">风速</td>
<td align="center"><%=speedV%>Kph</td>
</tr>
<tr>
<td align="right">湿度</td>
<td align="center"><%=humidityV%>%</td>
</tr>
<tr style="background-color:#E6F5FF">
<td align="right">能见度</td>
<td align="center"><%=visibility%>km</td>
</tr>
</table>
<table width="163" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2" align="center" class="time"><%=lastV%></td>
</tr>
<tr>
<td bgcolor="#FFFFCC">日出<%=sunriseV%></td>
<td bgcolor="#FFCC00">日落<%=sunsetV%></td>
</tr>
</table>
<table width="163" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<%
for(var i=0; i<2; i++){
var dayV=attri(forecastNode,i,"day")
var dateV=attri(forecastNode,i,"date")
var lowV=attri(forecastNode,i,"low")
var highV=attri(forecastNode,i,"high")
var texttV=attri(forecastNode,i,"text")
var codetV=attri(forecastNode,i,"code")
var dayCH=dayEn(dayV)
var texttCH=tEN2CH(texttV)
var weabmp=weaimg(codetV)
//var weatNum=weaimg(codetV)
%>
<td align="center"><%=dayCH%><br /><%=weabmp%><br /><%=texttCH%><br />最高<%=highV%>°<br />最低<%=lowV%>°</td>
<%
}
%>
</tr>
</table>
习惯了用JS写东西,一不小心ASP也全拿JS写的,呵呵,有时间再改成VB。
我再鄙视他一下,FLASH是N年前就搞过的,时间虽然丢的有点长了,在AW的帮助,慢慢就会有所起色。
总算平息了火,还算是又默默改变了一件事情。
代码如下:
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
<%
Session.CodePage = "65001"
Response.contentType="text/html"
Response.Expires = 0
var N=Request("n")
var Num=Request("id")
var url="http://xml.weather.yahoo./forecastrss?p="+N+"&u=c&id="+Num;
var xmldoc=Server.CreateObject("Microsoft.Xmlhttp");
xmldoc.open('GET',url,false);
xmldoc.send(null);
var response = xmldoc.responseXML.documentElement;
var lastNode=response.selectNodes("//rss/channel/lastBuildDate");
var cityNode=response.selectNodes("//rss/channel/yweather:location");
var windNode=response.selectNodes("//rss/channel/yweather:wind");
var atmosphereNode=response.selectNodes("//rss/channel/yweather:atmosphere");
var astronomyNode=response.selectNodes("//rss/channel/yweather:astronomy");
var conditionNode=response.selectNodes("//rss/channel/item/yweather:condition");
var forecastNode=response.selectNodes("//rss/channel/item/yweather:forecast");
var lastV=lastNode(0).firstChild.nodeValue
//编辑属性
function attri(a,b,c){
return a(b).attributes.getNamedItem(c).value
}
var cityV=attri(cityNode,0,"city")
var chillV=attri(windNode,0,"chill")
var directionV=attri(windNode,0,"direction")
var speedV=attri(windNode,0,"speed")
var humidityV=attri(atmosphereNode,0,"humidity")
var visibilityV=attri(atmosphereNode,0,"visibility")
var sunriseV=attri(astronomyNode,0,"sunrise")
var sunsetV=attri(astronomyNode,0,"sunset")
var textV=attri(conditionNode,0,"text")
var codeV=attri(conditionNode,0,"code")
var tempV=attri(conditionNode,0,"temp")
//转换英文为中文
var cityCH=EN2CH(cityV)
function EN2CH(obj){
if(obj=="Wuhan"){return "武汉"}
else if(obj=="Nanchang"){return "南昌"}
else if(obj=="Haikou"){return "海口"}
else if(obj=="Beijing"){return "北京"}
else if(obj=="Shanghai"){return "上海"}
else if(obj=="Guangzhou"){return "广州"}
else if(obj=="Yinchuan"){return "银川"}
else if(obj=="Zhengzhou"){return "郑州"}
else if(obj=="Changsha"){return "长沙"}
else if(obj=="Hanzhong"){return "杭州"}
else if(obj=="Hong Kong"){return "香港"}
else if(obj=="Xin'an"){return "西安"}
else if(obj=="Chengdu"){return "成都"}
else if(obj=="Qingdao"){return "青岛"}
else if(obj=="Guiyang"){return "贵阳"}
else if(obj=="Jinan"){return "济南"}
else if(obj=="Hefei"){return "合肥"}
else if(obj=="Fuzhou"){return "福州"}
else if(obj=="Nanjing"){return "南京"}
else{return obj}
}
//风向函数
var windCH=wind(directionV)
function wind(a){
if(a>337 && a<=360){return "北风"};
else if(a>=0 && a<=22){return "北风"};
else if(a>22 && a<=67){return "东北风"};
else if(a>67 && a<=112){return "东风"};
else if(a>112 && a<=157){return "东南风"};
else if(a>157 && a<=202){return "南风"};
else if(a>202 && a<=247){return "西南风"};
else if(a>247 && a<=292){return "西风"};
else if(a>292 && a<=337){return "西北风"};
}
//能见度函数
visibility=vis(visibilityV)
function vis(a){
return a/100
}
//转换天气为中文
var textCH=tEN2CH(textV)
function tEN2CH(obj){
if(obj=="Clear"){return "晴朗"}
else if(obj=="Sunny"){return "晴天"}
else if(obj=="Partly Cloudy"){return "局部有云"}
else if(obj=="Mostly Clear"){return "晴"}
else if(obj=="Mostly Sunny"){return "小云"}
else if(obj=="Mostly Cloudy"){return "多云"}
else if(obj=="Cloudy"){return "云"}
else if(obj=="Sunny/Wind"){return "晴转云"}
else if(obj=="AM Clouds/PM Sun/Wind"){return "云转晴"}
else if(obj=="AM Showers"){return "晨雨"}
else{return obj}
}
//天气图片
var weaNum=weaimg(codeV)
function weaimg(obj){
return "<img src=\"http\://us\.i1\.yimg\./us\.yimg./i/us/we/52/"+obj+"\.gif\" />"
}
//星期替换
function dayEn(obj){
if(obj=="Sun"){return "星期日"}
else if(obj=="Mon"){return "星期一"}
else if(obj=="Tue"){return "星期二"}
else if(obj=="Wed"){return "星期三"}
else if(obj=="Thu"){return "星期四"}
else if(obj=="Fri"){return "星期五"}
else if(obj=="Sat"){return "星期六"}
else{return obj}
}
%>
<table width="163" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td rowspan="6" align="center"><%=cityCH%><br />
<%=weaNum%><br />
<%=textCH%></td>
<td align="right">感觉</td>
<td align="center"><%=chillV%>°</td>
</tr>
<tr style="background-color:#E6F5FF">
<td align="right">实际</td>
<td align="center"><%=tempV%>°</td>
</tr>
<tr>
<td align="right">风向</td>
<td align="center"><%=windCH%></td>
</tr>
<tr style="background-color:#E6F5FF">
<td align="right">风速</td>
<td align="center"><%=speedV%>Kph</td>
</tr>
<tr>
<td align="right">湿度</td>
<td align="center"><%=humidityV%>%</td>
</tr>
<tr style="background-color:#E6F5FF">
<td align="right">能见度</td>
<td align="center"><%=visibility%>km</td>
</tr>
</table>
<table width="163" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2" align="center" class="time"><%=lastV%></td>
</tr>
<tr>
<td bgcolor="#FFFFCC">日出<%=sunriseV%></td>
<td bgcolor="#FFCC00">日落<%=sunsetV%></td>
</tr>
</table>
<table width="163" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<%
for(var i=0; i<2; i++){
var dayV=attri(forecastNode,i,"day")
var dateV=attri(forecastNode,i,"date")
var lowV=attri(forecastNode,i,"low")
var highV=attri(forecastNode,i,"high")
var texttV=attri(forecastNode,i,"text")
var codetV=attri(forecastNode,i,"code")
var dayCH=dayEn(dayV)
var texttCH=tEN2CH(texttV)
var weabmp=weaimg(codetV)
//var weatNum=weaimg(codetV)
%>
<td align="center"><%=dayCH%><br /><%=weabmp%><br /><%=texttCH%><br />最高<%=highV%>°<br />最低<%=lowV%>°</td>
<%
}
%>
</tr>
</table>
上一篇:用Ajax来控制书签和回退按钮的代码
下一篇:AJAX天气预报前台
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程