【问题标题】:XMLHttpRequest won't work in IE 7/8 but works in other browsersXMLHttpRequest 在 IE 7/8 中不起作用,但在其他浏览器中起作用
【发布时间】:2011-07-22 01:31:03
【问题描述】:

我开发了一个在 chrome 和 firefox 上运行良好的网络应用程序。但是,当涉及到测试时间时,它在 IE 中无法正常工作。它似乎并没有真正得到请求?

这里是 Javascript 代码:

function createXMLHttpRequest() {
    var xmlhttp = false;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else if(window.ActiveXObject) {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                xmlhttp = false;
            }
        }
    }
    return xmlhttp;
};




function checkForData(){

    var target = document.getElementById('accordion');
    var loading = document.getElementById('controls');

    var xhr = createXMLHttpRequest();


    loading.innerHTML = '<img src="../images/loading.gif" width=20 height=20 />Querying the Database';

    xhr.open('GET','getData.php',true);
    xhr.send(null); 

    xhr.onload = function(){
        loading.innerHTML = '<a href="../classes/php/print.php" />Print Report</a><br/><br/><a href="../index.php" />HomePage</a><br/><a href="../classes/php/actionedClass.php" />Refresh</a><br/><a href="../classes/php/logout.php" />Logout</a><br/>';

        target.innerHTML = xhr.responseText;
//      addPrettyness();

    };
    xhr.onerror = function (){
        target.innerHTML = 'Failed :(';
    };

}

function addPrettyness(){
    $(function() {
                    var stop = false;
                    $( "#accordion h3" ).click(function( event ) {
                        if ( stop ) {
                            event.stopImmediatePropagation();
                            event.preventDefault();
                            stop = false;
                        }
                    });
                    $( "#accordion" )
                            .accordion({
                                collapsible: true,
                                header: "> div > h3"});
    });
}

function checkAgain(){
    setInterval('checkForData()',30000);            //Every 30 seconds run the check for new data script

}


function changeAction(action, actionChangeId){

    xhr = new XMLHttpRequest();

    if(action == 0){


        var loading = document.getElementById('controls');
        loading.innerHTML = '<img src="../images/loading.gif" width=20 height=20 />Sending to the Database';        

        xhr.open('GET','../classes/php/actionNo.php?actionChangeId='+actionChangeId,true);
        xhr.send();

        xhr.onload = function(){
            checkForData();
        };

        xhr.onerror = function(){
            alert('Failed to change action... Try again please!');
        };

    }else if(action == 1){
        xhr = new XMLHttpRequest();

        var loading = document.getElementById('controls');
        loading.innerHTML = '<img src="../images/loading.gif" width=20 height=20 />Sending to the Database';        


        xhr.open('GET','../classes/php/actionYes.php?actionChangeId='+actionChangeId,true);
        xhr.send();

        xhr.onload = function(){
            checkForData();
        };

        xhr.onerror = function(){
            alert('Failed to change action... Try again please!');
        };
    }
}

function startEngine(){
    checkForData();
    //checkAgain();

}


window.onload = startEngine;

它从 echos 请求的 .php 文件将字符串结果返回给 javascript。

【问题讨论】:

  • 我知道这不是答案,但是 jQuery 呢?
  • 该对象在每个浏览器中的工作方式都不相同。如果你想支持你真的应该使用像 jQuery 这样的库。
  • 我已经使用过它,但我宁愿创建自己的一组小型库来了解更多信息,这也与 jQuery 库几乎相同,但在一个较小的文件中,因为这是根据我的需要指定的.我可能错过了一个逗号或忘记在某个地方放一个空值我只需要一双新的眼睛因为我受伤了:(大声笑。谢谢你的回复虽然芽:)
  • 自 1990 年代以来,该对象在 IE6 中就已经存在,它确实适用于每个浏览器:/
  • @Ash 虽然对象 已经 已经存在,但它在浏览器中仍然不同——正如您可以从 createXMLHttpRequest 函数中看出的那样,该函数尝试以三种不同的方式创建它,每个都可能成功,具体取决于运行代码的浏览器!

标签: php javascript internet-explorer-8 internet-explorer-7 xmlhttprequest


【解决方案1】:

here 所述,Internet Explorer 仅从版本 9 开始支持 XMLHttpRequest 对象的 onload 事件。

所以,对于 IE 8 及以下版本,你可以用老式的方式来做:

xhr.onreadystatechange = function()
{
    //ready?
    if (xhr.readyState != 4)
        return false;

    //get status:
    var status = xhr.status;

    //maybe not successful?
    if (status != 200) {
        alert("AJAX: server status " + status);
        return false;
    }

    //Got result. All is good.
    loading.innerHTML = '<a href="../classes/php/print.php" />Print Report</a>' + 
        '<br/><br/><a href="../index.php" />HomePage</a><br/>' + 
        '<a href="../classes/php/actionedClass.php" />Refresh</a><br/>' + 
        '<a href="../classes/php/logout.php" />Logout</a><br/>';
    target.innerHTML = xhr.responseText;

    return true;
}

【讨论】:

  • 嗯,是的,我想这才是真正的原因。
  • 啊!太棒了,我明白你的意思了。我很懒惰使用 onload 方法和d.....Shadow Wizard - You Rock。非常感谢所有帮助过的人!!!!!!
  • 干杯 @Ash,我很高兴我能挖掘出我古老的 AJAX 代码来获得准确的代码。 :)(没有压力,但如果您如此对此感到高兴,请随时投票:p)
  • 在你前面的朋友,非常感谢你! 2天的砖头撞墙,你把我从IE相关的终端隐士中救了出来! :D
  • 附言。我不想与任何人争论我只需要从头开始理解这一点......谢谢你们向我指出 jQuery 并感谢你们的回应:) 我为看起来如此消极而道歉!
【解决方案2】:

来自维基百科:

if (typeof XMLHttpRequest == "undefined")
  XMLHttpRequest = function () {
    try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
      catch (e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
      catch (e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (e) {}
    //Microsoft.XMLHTTP points to Msxml2.XMLHTTP.3.0 and is redundant
    throw new Error("This browser does not support XMLHttpRequest.");
  };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 2016-04-20
    • 2020-03-29
    • 2013-09-29
    • 1970-01-01
    • 2013-06-10
    相关资源
    最近更新 更多