Php internet explorer中的AJAX问题!(该代码目前适用于firefox和chrome)

Php internet explorer中的AJAX问题!(该代码目前适用于firefox和chrome),php,javascript,ajax,internet-explorer,dhtml,Php,Javascript,Ajax,Internet Explorer,Dhtml,因此,我有一个AJAX项目,它使用XmlHttpRequest对象从服务器端动态检索数据(在我的例子中,如果相关的话,我使用JSON和PHP/MySQL)。几乎我所有的HTML元素都是通过JavaScriptDOM动态创建的,因此需要.js文件来完成 下面是一个典型的.js文件,我使用它从PHP获取服务器端信息,然后构建html: var xmlHttp = createXmlHttpRequestObject(); function createXmlHttpRequestObject()

因此,我有一个AJAX项目,它使用XmlHttpRequest对象从服务器端动态检索数据(在我的例子中,如果相关的话,我使用JSON和PHP/MySQL)。几乎我所有的HTML元素都是通过JavaScriptDOM动态创建的,因此需要.js文件来完成

下面是一个典型的.js文件,我使用它从PHP获取服务器端信息,然后构建html:

var xmlHttp = createXmlHttpRequestObject(); 

function createXmlHttpRequestObject() {
    var xmlHttp; 
    try {
        xmlHttp = new XMLHttpRequest(); 
    } catch(e) {
        var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                            "MSXML2.XMLHTTP.5.0",
                            "MSXML2.XMLHTTP.4.0",
                            "MSXML2.XMLHTTP.3.0",
                            "MSXML2.XMLHTTP", 
                            "Microsoft.XMLHTTP"); 
        for(var i = 0; i < XmlHttpVersions.length && !xmlHttp; i++) {
            try {
                xmlHttp = new ActiveXObject(XmlHttpVersions[i]); 
            } catch(e) {} 
        }
    }
    if (!xmlHttp) alert("Error creating XmlHttpRequest object."); 
    else { return xmlHttp; } 
} 

function initialize_main() {
    if (xmlHttp) {
        try {

            xmlHttp.open("GET", "main_php.php", true); 
            xmlHttp.onreadystatechange = handleMainStateChange; //call a function when the state changes
            xmlHttp.send(null); 
        } catch(e) {
            alert("Can't connect to server: " + e.toString());
        } 
    }
}

function handleMainStateChange() {
    if (xmlHttp.readyState==4) {
        if (xmlHttp.status==200) {  
            try {
                init_main(); 
            } catch(e) {
                alert("Error reading the response: " + e.toString()); 
            }
        } else {
            alert("There was a problem retrieving data: " + xmlHttp.statusText); 
        }
    }
}

function init_main() {

var data = JSON.parse(xmlHttp.responseText); 

//now do stuff with the DOM or w/e 

}
var xmlHttp=createXmlHttpRequestObject();
函数createXmlHttpRequestObject(){
var-xmlHttp;
试一试{
xmlHttp=新的XMLHttpRequest();
}捕获(e){
var XmlHttpVersions=新数组(“MSXML2.XMLHTTP.6.0”,
“MSXML2.XMLHTTP.5.0”,
“MSXML2.XMLHTTP.4.0”,
“MSXML2.XMLHTTP.3.0”,
“MSXML2.XMLHTTP”,
“Microsoft.XMLHTTP”);
对于(var i=0;i

正如我所说,firefox和chrome中的一切都很酷。但是internet explorer告诉我:“读取响应时出错:TypeError:Object不支持此属性或方法”。正如你可能猜到的,我对AJAX有点陌生,所以谢谢你的帮助

尝试使用此功能,对我来说,它适用于所有浏览器

function getXMLHTTP()
{
    var xmlhttp = false;
    try
    {
        xmlhttp = new XMLHttpRequest();
    }
    catch(e)
    {
        try
        {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
            try
            {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e1)
            {
                xmlhttp = false;
            }
        }
    }
    return xmlhttp;
}

我强烈建议您使用JQuery,这样您就可以编写不关心浏览器类型的Javascript(JQuery为您做这件事):

我建议您使用.I second Tapos;重新发明轮子(特别是当涉及到Javascript时,在Javascript中存在如此多的跨浏览器不兼容)是疯狂的。使用任何库都可以做到这一点,但不要使用自己的库。哦,谢谢你的推荐,我当然会为我的下一个项目,但我想开始,因为这是我第一次接触javascript。不幸的是,以这种方式编写的代码大约有10k行,我认为这些代码不会在jquery中重写。。。所以我一直坚持(并且有点想弄清楚)用IE制作我的作品。嗯,这很奇怪,因为我认为我的版本尝试了你尝试的两种ActiveXObject组合(再加上一些),但它们似乎都不适用于IE。在我最初的for循环中是否有一些愚蠢的事情我在做/缺少?