没学习AJAX之前,最在想如何调用别人网页的内容,页且还要过滤其它不需要的内容,如何实现了,学到异步调用后,就可以实现了,闲话少话,先看代码;
//JScript.js
1
//
JScript 文件
2
3 var xmlhttp;
4 function getWeather()
5 {
6 //获取用户输入的城市名称
7 var mycity=document.getElementById("txtCity").value;
8 //创建异步对象
9 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
10 //加载服务器-注意URL参数的使用
11
12 xmlhttp.Open("GET","http://php.weather.sina.com.cn/search.php?city="+mycity,true)
13 xmlhttp.onreadystatechange=stateChange;
14 //发送请求-无参数
15 xmlhttp.Send();
16 }
17 function stateChange()
18 {
19 if(xmlhttp.readystate==4 && xmlhttp.status==200)
20 {
21 //获取所有返回的数据
22 var data=bytes2BSTR(xmlhttp.ResponseBody);
23
24 //过滤自己需要的数据
25 var begin=data.indexOf("天气状况 begin");
26 var end=data.indexOf("天气状况 end");
27 var weather=data.substring(begin+15,end);
28 //填充天气内容
29 document.getElementById("divweather").innerHTML=weather;
30 //显示结果
31 document.getElementById("divweather").style.visibility="visible";
32 }
33 }
2
3 var xmlhttp;
4 function getWeather()
5 {
6 //获取用户输入的城市名称
7 var mycity=document.getElementById("txtCity").value;
8 //创建异步对象
9 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
10 //加载服务器-注意URL参数的使用
11
12 xmlhttp.Open("GET","http://php.weather.sina.com.cn/search.php?city="+mycity,true)
13 xmlhttp.onreadystatechange=stateChange;
14 //发送请求-无参数
15 xmlhttp.Send();
16 }
17 function stateChange()
18 {
19 if(xmlhttp.readystate==4 && xmlhttp.status==200)
20 {
21 //获取所有返回的数据
22 var data=bytes2BSTR(xmlhttp.ResponseBody);
23
24 //过滤自己需要的数据
25 var begin=data.indexOf("天气状况 begin");
26 var end=data.indexOf("天气状况 end");
27 var weather=data.substring(begin+15,end);
28 //填充天气内容
29 document.getElementById("divweather").innerHTML=weather;
30 //显示结果
31 document.getElementById("divweather").style.visibility="visible";
32 }
33 }
//VBScript.vbs
1
function
bytes2BSTR(vIn)
2 dim strReturn,i,ThisCharCode,innerCode,Hight8,Low8,NextCharCode
3 strReturn = ""
4 for i = 1 to LenB(vIn)
5 ThisCharCode = AscB(MidB(vIn,i, 1 ))
6 if ThisCharCode <& H80 Then
7 strReturn = strReturn & Chr (ThisCharCode)
8 else
9 NextCharCode = AscB(MidB(vIn,i + 1 , 1 ))
10 strReturn = strReturn & Chr ( CLng (ThisCharCode) *& H100 + CInt (NextCharCode))
11 i = i + 1
12 end if
13 next
14 bytes2BSTR = strReturn
15 end function
16
2 dim strReturn,i,ThisCharCode,innerCode,Hight8,Low8,NextCharCode
3 strReturn = ""
4 for i = 1 to LenB(vIn)
5 ThisCharCode = AscB(MidB(vIn,i, 1 ))
6 if ThisCharCode <& H80 Then
7 strReturn = strReturn & Chr (ThisCharCode)
8 else
9 NextCharCode = AscB(MidB(vIn,i + 1 , 1 ))
10 strReturn = strReturn & Chr ( CLng (ThisCharCode) *& H100 + CInt (NextCharCode))
11 i = i + 1
12 end if
13 next
14 bytes2BSTR = strReturn
15 end function
16
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>XmlHttpRequest异步调用实现天气预报的调用</title>
/**//* 关键的两句先进行引用写好的异步调用脚本*/
<script type="text/vbscript" src="VBScript.vbs"></script>
<script type="text/javascript" src="JScript.js"></script>
</head>
<body>
<form id="form1" runat="server">
<table style="width: 627px; height: 204px">
<tr height="30%">
<td >
输入城市</td>
<td >
<asp:TextBox ID="txtCity" runat="server" Text="武
汉"></asp:TextBox></td>
</tr>
<tr height="30%">
<td >
</td>
<td >
<input id="btnWeather" type="button" value="天气情况"
onclick="getWeather()" /></td>
</tr>
<tr height="40%">
<td colspan="2" style="height: 31px">
<div id="divweather"></div>
</td> </tr>
</table>
</form>
</body>
</html>
有不对写的不好的地方 请园子里的各位高手多多指教,提<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>XmlHttpRequest异步调用实现天气预报的调用</title>
/**//* 关键的两句先进行引用写好的异步调用脚本*/
<script type="text/vbscript" src="VBScript.vbs"></script>
<script type="text/javascript" src="JScript.js"></script>
</head>
<body>
<form id="form1" runat="server">
<table style="width: 627px; height: 204px">
<tr height="30%">
<td >
输入城市</td>
<td >
<asp:TextBox ID="txtCity" runat="server" Text="武
汉"></asp:TextBox></td>
</tr>
<tr height="30%">
<td >
</td>
<td >
<input id="btnWeather" type="button" value="天气情况"
onclick="getWeather()" /></td>
</tr>
<tr height="40%">
<td colspan="2" style="height: 31px">
<div id="divweather"></div>
</td> </tr>
</table>
</form>
</body>
</html>
输入城市名称后,点击按钮显示如下结果: