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