读书人

入门简单AJAX有关问题

发布时间: 2012-10-18 13:46:56 作者: rapoo

入门简单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()好像就没用到,这应该是个失误吧

读书人网 >Ajax

热点推荐