日期:2014-05-16  浏览次数:20639 次

入门简单AJAX问题
HTML code
<html>
  <head>
    <title>createAjax.html</title>
  </head>  
  <body>
     <script language="Javascript">
         var xmlHttp;
         function createXMLHttp(){
             if(window.XMLHttpRequest){
                 xmlHttp=new XMLHttpRequest();
             }else{
                 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
             }
         }
         function showMsg(){
             xmlHttp.open("POST","content.html");
             xmlHttp.onreadystatechange=showMsgCallback();
             xmlHttp.send(null);
         }
         function showMsgCallback(){
             if(xmlHttp.readyState == 4){
                 if(xmlHttp.status == 200){
                     var text=xmlHttp.responseText;
                     document.getElementById("msg").className="样式表名称";
                     document.getElementById("msg").innerHTML=text;
                 }
             }
         }
         </script>
         <input type="button" onclick="showMsg()" value="调用AJAX显示内容"/>
         <span id="msg"></span>
  </body>
</html>



content.html

HTML code
hello world!!!


效果出不来。错误也找不出。

------解决方案--------------------
错误在于楼主的showMsg方法之中,
JScript code

function showMsg(){ 
    xmlHttp.open("POST","content.html"); 
    //xmlHttp.onreadystatechange=showMsgCallback(); 
   xmlHttp.onreadystatechange=showMsgCallback;//这句才是正确的写法,去掉了showMsgCallback后的括号 
    xmlHttp.send(null); 
}

------解决方案--------------------
除了楼上各位的建议,楼主可以看看你的createXMLHttp()好像就没用到,这应该是个失误吧