XMLHttpRequest在IE8标准文档模式下不适用于32位机器,IE10

时间:2014-12-29 19:54:51

标签: javascript internet-explorer xmlhttprequest 32bit-64bit x-ua-compatible

想知道是否有人在此之前遇到过这个奇怪的IE / javascript错误。

在IE8标准文档模式下使用IE10的32位计算机上,javascript在尝试创建新的XMLHttpRequest对象时返回TypeError。这是一个问题,因为我们的一个页面通过X-UA兼容的IE = 8元标记强制IE8标准(页面的要求)。

新窗口.XMLHttpRequest();
TypeError:对象不支持此操作

来自64位计算机(IE8标准中的IE10)的完全相同的代码行正常工作。

32位IE10 IE8标准
32-bit IE10 IE8 standards

64位IE10 IE8标准
64-bit IE10 IE8 standards

1 个答案:

答案 0 :(得分:1)

我使用angularjs版本1.2.9遇到了类似的问题。事实证明,angular并不能最好地检测window.XMLHttpRequest()的可用性。 jQuery的方法更加彻底。

angularjs 1.2.9

function createXhr(method) {
   // IE8 doesn't support PATCH method, but the ActiveX object does
   /* global ActiveXObject */
   return (msie <= 8 && lowercase(method) === 'patch')
      ? new ActiveXObject('Microsoft.XMLHTTP')
      : new window.XMLHttpRequest();
}

jQuery 1.10.2

// Functions to create xhrs
function createStandardXHR() {
    try {
        return new window.XMLHttpRequest();
    } catch( e ) {}
}

function createActiveXHR() {
    try {
        return new window.ActiveXObject("Microsoft.XMLHTTP");
    } catch( e ) {}
}

// Create the request object
// (This is still attached to ajaxSettings for backward compatibility)
jQuery.ajaxSettings.xhr = window.ActiveXObject ?
    /* Microsoft failed to properly
    * implement the XMLHttpRequest in IE7 (can't request local files),
    * so we use the ActiveXObject when it is available
    * Additionally XMLHttpRequest can be disabled in IE7/IE8 so
    * we need a fallback.
    */
    function() {
        return !this.isLocal && createStandardXHR() || createActiveXHR();
    } :
    // For all other browsers, use the standard XMLHttpRequest object
    createStandardXHR;

// Determine support properties
xhrSupported = jQuery.ajaxSettings.xhr();

对我来说,解决方法是添加一个额外的条件,检查IE8文档模式为angular的createXhr方法:

function createXhr(method) {
   // IE8 doesn't support PATCH method, but the ActiveX object does
   /* global ActiveXObject */
   return ((msie <= 8 && lowercase(method) === 'patch') ||
        (msie >= 8 && document.documentMode == 8))
      ? new ActiveXObject('Microsoft.XMLHTTP')
      : new window.XMLHttpRequest();
}

另一种方法是实现jQuery的方法,该方法可以查看ActiveXObject是否可用。如果它然后尝试创建标准的XMLHttpRequest,如果失败,它将回退到ActiveX替代方案。

相关问题