设为首页
加入收藏
站内地图
旧版入口
当前位置:首页 > 站长学院 > 网络编程 > ASP.NET

创建不了XMLHTTP控件

作者:佚名 出处:网络转载 时间:11-22 点击:

内容载入中...
     最近在用ajax开发服务器程序,发现IE浏览器不支持XMLhttprequest对象,而且找不到Microsoft.XMLHTTP控件。
  
  问题出现了我们需要解决,解决方案如下:
  
  1、运行下regsvr32 msXML3.dll;
  2、用现成的框架来做ajax;
  3、代码优化:
  
  if(window.ActiveXObject)
   {
   XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   else if(window.XMLHttpRequest)
   {
   XMLHttp = new XMLHttpRequest();
   }
  
   if(handle_s == null)
   handle_s = "bin/normal.py/db";
   this.XMLHttp.onreadystatechange = handle_l;
   this.XMLHttp.open("GET",handle_s,true);
   this.XMLHttp.send(null);
  
  或判断浏览器
  
  var agt = navigator.userAgent.toLowerCase();
  var is_ie = (agt.indexOf("msie") != -1);
  var is_ie5 = (agt.indexOf("msie 5") != -1);
  var is_opera = (agt.indexOf("opera") != -1);
  var is_mac = (agt.indexOf("mac") != -1);
  var is_gecko = (agt.indexOf("gecko") != -1);
  var is_safari = (agt.indexOf("safari") != -1);
  
  function CreateXMLHttpReq(handler) {
  
  var XMLhttp = null;
  if (is_ie) {
  // Guaranteed to be ie5 or ie6
  var control = (is_ie5) ? "Microsoft.XMLHTTP" : "MsXML2.XMLHTTP";
  
  try {
  XMLhttp = new ActiveXObject(control);
  XMLhttp.onreadystatechange = handler;
  } catch (ex) {
  // TODO: better help message
  alert("You need to enable active scripting and activeX controls");
  }
  
  } else {
  
  // Mozilla
  XMLhttp = new XMLHttpRequest();
  XMLhttp.onload = handler;
  XMLhttp.onerror = handler;
  
  }
  
  return XMLhttp;
  }
  
  或者
  
  <script language="javascript">
   var http_request = false;
   function send_request(url) {//初始化、指定处理函数、发送请求的函数
   http_request = false;
   //开始初始化XMLHttpRequest对象
   if(window.XMLHttpRequest) { //Mozilla 浏览器
   http_request = new XMLHttpRequest();
   if (http_request.overrideMimeType) {//设置MiME类别
   http_request.overrideMimeType('text/XML');
   }
   }
   else if (window.ActiveXObject) { // IE浏览器
   try {
   http_request = new ActiveXObject("MsXML2.XMLHTTP");
   } catch (e) {
   try {
   http_request = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (e) {}
   }
   }
   if (!http_request) { // 异常,创建对象实例失败
   window.alert("不能创建XMLHttpRequest对象实例.");
   return false;
   }
   http_request.onreadystatechange = processRequest;
   // 确定发送请求的方式和URL以及是否同步执行下段代码
   http_request.open("GET", url, true);
   http_request.send(null);
   }
   // 处理返回信息的函数
   function processRequest() {
   if (http_request.readyState == 4) { // 判断对象状态
   if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
   var returnObj = http_request.responseXML;
   var XMLobj = http_request.responseXML;
   var employees = XMLobj.getElementsByTagName("employee");
   var feedbackStr = "";
   for(var i=0;i<employees.length;i++) { // 循环读取employees.XML的内容
   var employee = employees[i];
   feedbackStr += "员工:" + employee.getAttribute("name");//取得标签指定属性
   feedbackStr += " 职位:" + employee.getElementsByTagName("job")[0].firstChild.data;//取得指定标签的最初数据
   feedbackStr += " 工资:" + employee.getElementsByTagName("salary")[0].firstChild.data;
   feedbackStr += "\r\n";
   }
   alert(feedbackStr);
   } else { //页面不正常
   alert("您所请求的页面有异常。");
   }
   }
   }
  </script>
  http://www.cnblogs.com/skylaugh/archive/2006/11/20/566164.HTML
  
  
    。

收藏本文:
】【打印页面】【推荐给朋友】【关闭窗口

站长学院

推荐信息