日期:2014-05-17  浏览次数:20507 次

关于ajax中在javascript的构造httpRequest的问题
<script type="text/javascript">
  function getUserList() {
  var httpRequest;
  if (window.ActiveXObject) {
  httpRequest = new ActiveXObject("Microsoft.XMLDOM");
  } else {
  httpRequest = new XMLHttpRequest();
  }
  var url = "handler.ashx";
  httpRequest.open("POST", url, true); //程序运行到这里就报错说 此对象不支持此属性或者用法
  httpRequest.send(null);
  httpRequest.onreadystatechange = function () {
  if (httpRequest.readyState == 4) {
  alert(httpRequest.readyState + " " + httpRequest.status);
  if (httpRequest.status == 200) {
  var dom = httpRequest.responseXML;
  document.getElementById("one").innerHTML = dom.getElementsByTagName[0].text;
  document.getElementById("two").innerHTML = dom.getElementsByTagName[1].text;
  document.getElementById("three").innerHTML = dom.getElementsByTagName[2].text;
  document.getElementById("four").innerHTML = dom.getElementsByTagName[3].text;
  document.getElementById("five").innerHTML = dom.getElementsByTagName[4].text;
  }
  }
  };
   
  };
  </script>

求大牛们指点,好纠结啊。

------解决方案--------------------
if (window.ActiveXObject) {
httpRequest = new ActiveXObject("Microsoft.XMLDOM");
} else {
httpRequest = new XMLHttpRequest();
}

最佳做法是改成

 if (window.XMLHttpRequest) {
httpRequest = new XMLHttpRequest();
} else {

httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}

注意不是XMLDOM