配合AJAX天气预报的webService 之asp
程序员文章站
2022-03-31 14:00:59
因为发现ajax目前跨域有点问题,只能在ie里面设置才可以使用,所以自己就写了个简单asp的webservice,其实作用就是一个小偷程序,把雅虎的天气抓了过来,然后生成x...
因为发现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.com/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\.com/us\.yimg.com/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.com/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\.com/us\.yimg.com/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天气预报的webService 之asp
-
菜鸟蔡之Ajax复习第一篇(后台asp.net)(传统的JavaScript方法实现Ajax功能)
-
jQuery Ajax方法调用 Asp.Net WebService 的详细实例代码_jquery
-
asp.net下使用jquery 的ajax+WebService+json 实现无刷新取后台值的实现代码_jquery
-
jQuery Ajax方法调用 Asp.Net WebService 的详细实例代码
-
菜鸟蔡之Ajax复习第一篇(后台asp.net)(传统的JavaScript方法实现Ajax功能)
-
jQuery Ajax方法调用 Asp.Net WebService 的详细实例代码
-
asp.net下使用jquery 的ajax+WebService+json 实现无刷新取后台值的实现代码_jquery
-
配合AJAX天气预报的webService 之asp