代码var xmlHttp;
if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject){
xmlHttp = new ActiveXObject();
}
function method1(){
xmlHttp.open("GET","page.php",true);
xmlHttp.onreadystatechange = function (){
document.getElementById('show').innerHTML += document.myForm.word.value+"<br>";
}
xmlHttp.send(null);
}如图在火狐里就能显示,可是为什么在遨游里就不能显示。还有,我只想让它输出一次,可是为什么输出了三次,求解。

解决方案 »

  1.   

    获取ajax对象,除了分浏览器,IE非IE,还要IE内核的浏览器下,根据 msxml版本,通常以3为界,分为两种你可以试试new ActiveXObject("Microsoft.XMLHTTP");  3以下版本是xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");============
    为什么多次执行,你可以试试document.getElementById('show')……之后,加一句alert(1);就明白了,这玩意不止执行一次
      

  2.   

    一楼,先谢谢了。的确,加了alert(1),出现了3次提示。可这是怎么回事呢,求原理,解决方法
      

  3.   

    var xmlHttp = false;
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
        xmlHttp = false;
      }
    }
    if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
      xmlHttp = new XMLHttpRequest();
    }
     xmlHttp.onreadystatechange = updatePage;
    function updatePage() {
      if (xmlHttp.readyState == 4) {
        var response = xmlHttp.responseText;
        document.getElementById("zipCode").value = response;
      }
    }
    之所以执行几次,是因为ajax请求过程中,会有不同的状态
    readyState:
    0 (未初始化): (XMLHttpRequest)对象已经创建,但还没有调用open()方法。
    1 (载入):已经调用open() 方法,但尚未发送请求。
    2 (载入完成): 请求已经发送完成。
    3 (交互):可以接收到部分响应数据。
    4 (完成):已经接收到了全部数据,并且连接已经关闭。
      

  4.   

    再次询问一下3楼同学,ajax的每个状态,都会执行一遍onreadystatehange后的方法吗,这是为什么呀
      

  5.   

    会的
    所在可以在方法中做相应的处理xmlHttp.onreadystatechange = function (){
    if (xmlHttp.readyState == 0) {
        
      }
    if (xmlHttp.readyState == 1) {
        
      }
    .........
    if (xmlHttp.readyState == 4) {
        
      }
    }
    具体可以看下 ibm 的这篇文章:
    http://www.ibm.com/developerworks/cn/xml/wa-ajaxintro1.html