日期:2014-05-16  浏览次数:20823 次

ajax 新建xmlHttpRequest对象问题 - Web 开发 / Ajax
var xmlHttp;
function createXmlHttpRequest() {
//1、创建xmlHttp对象
if (window.ActiveXObject) {
var activeXObjectName = ["Microsoft.XMLHTTP", "Msxml2.XMLHTTP"];
// for(var i = 0;i<activeXObjectName.length;i++)
// {
//alert("ActiveObject");
// try
// {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
// return ;
// }catch(e)
// {
// }
// }
alert("ActiveObject==" + xmlHttp);
} else {
if (window.XMLHttpRequest) {
//IE7 IE8 fireforx...
alert("xmlHttpRequest");
xmlHttp = new XMLHttpRequest();
}
}
}

红色字在JS中提示找不到。。xmlHttp对象就新建不了了。各位大哥帮个忙呀。。小弟在线等 。。急呀!!!!

------解决方案--------------------
JScript code
function ajaxFunction()
 {
 var xmlHttp;
 
 try
    {
   // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
 catch (e)
    {

  // Internet Explorer
   try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
   catch (e)
      {

      try
         {
         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
      catch (e)
         {
         alert("您的浏览器不支持AJAX!");
         return false;
         }
      }
    }
 }
</script>

------解决方案--------------------
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttp = false;
}
}

------解决方案--------------------
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}

------解决方案--------------------
这是从IBM官方网站上COPY来的代码,我一直在用,一直没问题
JScript code
function GetHttpRequest() {
    var xmlHttp = false;
    /*@cc_on@*/
    /*@if (@_jscript_version >= 5)
    try {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e2) {
            xmlHttp = false;
        }
    }
    @end@*/
    if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
        xmlHttp = new XMLHttpRequest();
    }
    return xmlHttp;
}