分享

兼容firefox3,ie,firefox2的xmlhttp请求写法

 跃来跃去 2009-02-16
最近安装了firefox3以后突然发现之前写的一些ajax请求无法正常的去获取数据了。于是赶快从网上找解决方法。终于在一个僻静的小角落找到了解决方法。马上写出共享之~积德啊积德~
 

//创建xmlhttp。此写法兼容firefox2,firefox3和ie。   
function createXMLHTTP() {
 var xmlhttp = null;
 if(window.ActiveXObject) {
  try {xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");}
  catch(e){try{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
  catch(e){}}
 } else if(window.XMLHttpRequest) {
  xmlhttp = new XMLHttpRequest();
  if(xmlhttp.overrideMimeType) {
   xmlhttp.overrideMimeType('text/xml');
  }
 } else {alert("Create XMLHttpRequest object false!");return false;}
    return xmlhttp;
}

调用处写法:

 var xmlhttp = createXMLHTTP();
 xmlhttp.open("GET","your request url",false);
    xmlhttp.send(null);
    if(xmlhttp.status == 200) {
     if(xmlhttp.responseText.length !== 0) {
         //此判断的内容为以前的回调函数。不明白为啥firefox3就成了这个样子~
        }   
    }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多