Internet Explorer 中的 AJAX 问题! (代码目前适用于 Firefox 和 Chrome)

发布于 2024-11-09 01:56:26 字数 2000 浏览 2 评论 0原文

因此,我有一个 AJAX 项目,它使用 XmlHttpRequest 对象从服务器端动态检索数据(在我的例子中,我将 JSON 与 PHP/MySQL 结合使用,以防相关)。几乎所有 HTML 元素都是通过 javascript DOM 动态创建的,因此是 .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 

}

正如我所说,在 Firefox 和 Chrome 中一切都很酷。但 Internet Explorer 告诉我:“读取响应时出错:TypeError:对象不支持此属性或方法”。正如您可能猜到的那样,我对 AJAX 有点陌生,所以感谢您的帮助!

So I've got an AJAX project which uses the XmlHttpRequest object to dynamically retrieve data from the server-side (in my case, I use JSON with PHP/MySQL in case that's relevant). Pretty much all my HTML elements are created dynamically via the javascript DOM, so it's the .js files doing the work.

Here's a typical .js file I use to get server-side info from PHP and then build the 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 

}

So as I said everything is cool in firefox and chrome. But internet explorer tells me: "Error reading the response: TypeError: Object doesn't support this property or method". I'm a bit new to AJAX as you might guess, so thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏闹i 2024-11-16 01:56:26

我强烈建议您使用 JQuery,这样您就可以编写不关心浏览器类型的 Javascript(JQuery 会为您做到这一点): http://jquery.com/

I would highly recommend that you use JQuery so that you can write Javascript that doesn't care about the type of browser (JQuery does this for you): http://jquery.com/

冬天的雪花 2024-11-16 01:56:26

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

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;
}

Try using this function, for me it works for all browser

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文