使用xmlHttp结合ASP实现网页的异步调用
作者:
通过xmlHttp和ASP的结合,我们可以轻松完成网页的异步调用。
代码如下:
1.新建Display.asp(这是前台显示页面)
注意xmlhttp.readyState的4个属性
1:LOADING;2:LOADED;3:INTERACTIVE;4:COMPLETED
<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<script language="javascript">
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
function fnDo(ID)
{
var xmlDom = new ActiveXObject("Msxml2.DOMDocument");
var strURL = "GetInfo.asp?ID=" + ID;
xmlhttp.Open("POST",strURL , true);
xmlhttp.onreadystatechange = fnRun;
xmlhttp.Send(xmlDom);
divTest.innerHTML = "Loading..."
}
//--------------------------------------------------------
function fnRun()
{
var state = xmlhttp.readyState;
var xmlDom = new ActiveXObject("Msxml2.DOMDocument");
if (state == 4)
{
xmlDom.loadXML(xmlhttp.responseXML.xml);
//alert(xmlDom.documentElement.selectSingleNode("//objXML").text)
var getInfo = xmlDom.documentElement.selectSingleNode("//objXML").text;
divTest.innerHTML = getInfo
}
}
</script>
<BODY>
<input type=text id=txtInput>
<input type=button value="异步调用" onclick="fnDo(document.all.txtInput.value)">
<DIV id=divTest></DIV>
<P> </P>
</BODY>
</HTML>
2.在建立GetInfo.asp(这是后台处理页面)
<%
Dim sID,objResult
sID = Trim(Request("ID"))
'sID = 28
Set objResult = Server.CreateObject("MSXML2.DOMDocument")
objResult.loadXML ("<objXML></objXML>")
'**************************************************************
'**************************************************************
objResult.selectSingleNode("objXML").text = "Get:" & sID
Response.ContentType = "text/xml"
objResult.save (Response)
Response.End
Set objSch = Nothing
Set objResult = Nothing
%>
3.运行Display.asp页面,在文本框里输入内容,点击按钮,可以看到Loading的提示,随后在不刷新页面的情况下得到了文本框里的内容。当然你也可以在GetInfo.asp那个页面里根据发送的参数做一些复杂的出来,随后把结果返回出来