就是因为我做的第一个例子都运行不出效果,看其他的参考书的例子也一样,结果都无法运行出效果。但是我现在想学学,因此问问<html>
<head>
<title>例子</title>
</head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312"/>
<script language=javascript>
var xmlhttp;
if(window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
xmlhttp=new XMLHttpRequest();
}
function makerequest(serverpage,objID)
{
var obj=document.getElementById(objID);
xmlhttp.open("GET",serverpage);
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
obj.innerHTML=xmlhttp.responseText;
}
}
xmlhttp.send(null);
}
</script>
<body onload="makerequest('content1.html','hw')">
<div align="center">
<h1>MY WEBpage</h1>
<span style="cursor:hand;" onclick="makerequest('content1.html','hw');">Page 1</span>
|<span style="cursor:hand;" onclick="makerequest('content2.html','hw');">Page 2</span>
|<span style="cursor:hand;" onclick="makerequest('content3.html','hw');">Page 3</span>
<div id="hw"></div>
</div>
</body>
</html>大家看这程序有没有问题?????????????

解决方案 »

  1.   


    <span style="cursor:hand;" onclick="makerequest('content1.html','hw');">Page 1</span>
    |<span style="cursor:hand;" onclick="makerequest('content2.html','hw');">Page 2</span>
    |<span style="cursor:hand;" onclick="makerequest('content3.html','hw');">Page 3</span>静态网页管用么?ajax本来就是异步刷新,你请求静态网页不管用的
      

  2.   

    调试的时候你是用什么方式访问网页的?本地方式访问时
    if(xmlhttp.readyState==4&&xmlhttp.status==200)
    条件不会成立
      

  3.   

    .html也不代表静态,完全可以是rewrite或者,服务器端定时生成的一个静态文件。ajax是可以这样子访问的
      

  4.   

    #7楼你可以试试,直接在windows操作系统资源管理器打开网页(浏览器路径显示类似file:///C:/...),不是通过http://ip/....方式,xmlhttp.status返回值不会是200的
      

  5.   

    我把xmlhttp.status==200去掉就行。原因未知
      

  6.   

    你要放在Web服务器上,用浏览器打开http地址,这样xmlhttp的status才可能返回200
    用file:///这样访问是不行的,根本是http请求。
      

  7.   

    那么你知道什么是http协议吗?
      

  8.   

    楼主可以参考学习一下jquery框架,比较容易
    $.ajax(
         url:"content1.html",
         dateType:"html",
         success:function(data,Status){
                     alert(data);
                 }
         )
      

  9.   

    把if(xmlhttp.readyState==4&&xmlhttp.status==200)这个改为:if(xmlhttp.readyState==4||xmlhttp.status==200)试一下