XMLHttpRequest异步调用实现调用新浪天气预报页面
没学习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
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
3
4
5
6
7
8
9
10
11
12
13
14
15
16
输入城市名称后,点击按钮显示如下结果:
转载于:https://www.cnblogs.com/accpxcb/archive/2007/12/15/AJax_weather-forecast.html