52,780
社区成员
发帖
与我相关
我的任务
分享<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!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 GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
function GetResult(){
var linkurl="/test/ajax.asp";
if(linkurl==""){return false;}
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){alert ("您的浏览器不支持AJAX!"); return;}
xmlHttp.open("GET",linkurl,false);
xmlHttp.onreadystatechange = callback;//触发callback事件,以判断信息是否正确
xmlHttp.send(null);
}
function callback(){
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var rs=xmlHttp.responseText;
//alert(rs);
if (rs!=null){
document.getElementById("content").innerHTML=rs;
}
else{alert("对不起!发生错误了° △ °|||")}
}
}
}
</script>
</head>
<body>
<div><a href="javascript:void(0);" onclick="GetResult();">click me!</a></div>
<div id="content"></div>
</body>
</html><%response.Charset="gb2312"
Response.Expires=0
str="<h1>I LOVE YOU FOREVER!</h1>"
response.Write str
%>
有点问题,是你的变量没有全局
1、
var xmlHttp;//下面的回调用到了这个变量
function GetResult(){
var linkurl="/test/ajax.asp";
if(linkurl==""){return false;}
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){alert ("您的浏览器不支持AJAX!"); return;}
xmlHttp.open("GET",linkurl,false);
xmlHttp.onreadystatechange = callback;//触发callback事件,以判断信息是否正确
xmlHttp.send(null);
}
function callback(){
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var rs=xmlHttp.responseText;
//alert(rs);
if (rs!=null){
document.getElementById("content").innerHTML=rs;
}
else{alert("对不起!发生错误了° △ °|||")}
}
}
}
2、
function GetResult(){
var linkurl="/test/ajax.asp";
if(linkurl==""){return false;}
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){alert ("您的浏览器不支持AJAX!"); return;}
xmlHttp.open("GET",linkurl,false);
xmlHttp.onreadystatechange = function () {//这样写就不会有xmlHttp变量作用域问题
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var rs=xmlHttp.responseText;
//alert(rs);
if (rs!=null){
document.getElementById("content").innerHTML=rs;
}
else{alert("对不起!发生错误了° △ °|||")}
}
}
}
xmlHttp.send(null);
}
3、
function GetResult(){
var linkurl="/test/ajax.asp";
if(linkurl==""){return false;}
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){alert ("您的浏览器不支持AJAX!"); return;}
xmlHttp.open("GET",linkurl,false);
xmlHttp.onreadystatechange = function () {
callback(xmlHttp);//传入参数
};//触发callback事件,以判断信息是否正确
xmlHttp.send(null);
}
function callback(xmlHttp){
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var rs=xmlHttp.responseText;
//alert(rs);
if (rs!=null){
document.getElementById("content").innerHTML=rs;
}
else{alert("对不起!发生错误了° △ °|||")}
}
}
}