XMLHttpRequest won't work in IE 7/8 but works in other browsers

前端 未结 2 848
时光取名叫无心
时光取名叫无心 2021-01-12 16:38

I\'ve developed a web application that works well on chrome and firefox. However when it came to testing time and it doesn\'t work properly in IE. It just doesn\'t seem to a

相关标签:
2条回答
  • 2021-01-12 17:06

    From Wikipedia:

    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.");
      };
    
    0 讨论(0)
  • 2021-01-12 17:15

    As mentioned here, Internet Explorer supports the onload event of the XMLHttpRequest object only since version 9.

    So, for IE 8 and below, you can do it in the old fashioned way:

    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;
    }
    
    0 讨论(0)
提交回复
热议问题