ajax简单示例
先要用 JavaScript 创建对象,代码:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%Response.Charset = "GB2312"%>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("您的浏览器不支持AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.getElementById("xs").innerHTML=xmlHttp.responseText;
}
}
xmlHttp.open("GET","ff.asp?oow="+document.getElementById("username").value,true);
xmlHttp.send(null);
}
</script>
</head>
<body>
用户名: <input type="text" name="username" id="username" onkeyup="ajaxFunction()" />
<span id="xs" style="color:#ff0000;"></span>
</body>
</html>
在 ff.asp 页面:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%Response.Charset = "GB2312"%>
<%
response.expires=-1
if len(request.QueryString("oow")) <=2 then
response.write("应大于两个字符")
end if
%>
注:下面这些浏览器定制的代码很长。不过,每当您希望创建 XMLHttpRequest 对象时,这些代码就能派上用场,因此您可以在任何需要使用的时间拷贝粘贴这些代码。下面这些代码兼容所有的主流浏览器:Internet Explorer、Opera、Firefox 以及 Safari。
<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("您的浏览器不支持AJAX!");
return false;
}
}
}
}
</script>