2

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

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

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

来自 64 位机器(IE8 标准中的 IE10)的完全相同的代码行工作得很好。

32 位 IE10 IE8 标准
32 位 IE10 IE8 标准

64 位 IE10 IE8 标准
64 位 IE10 IE8 标准

4

1 回答 1

1

我使用 angularjs 1.2.9 版遇到了类似的问题。事实证明,Angular 在检测 window.XMLHttpRequest() 的可用性方面做得不是最好的。jQuery 在他们的方法上更加彻底。

角度js 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();

对我来说,解决方法是在 Angular 的 createXhr 方法中添加一个检查 IE8 文档模式的附加条件:

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 替代方案。

于 2014-12-30T15:25:55.673 回答