javascript - 检测浏览器是否不支持具有完整 CORS 支持的 XMLHttpRequest

标签 javascript internet-explorer-8 xmlhttprequest xdomainrequest

如何检查浏览器是否支持带有 CORS 的 native JavaScript XMLHttpRequest 对象?我正在 IE 8 中进行测试,据我了解,它仅支持 XDomainRequest,但是在 IE 8 中执行以下操作:

typeof window.XMLHttpRequest
"object"

我本以为这会是未定义。基本上,我如何检测浏览器是否支持完整的 XMLHttpRequest 以及 CORS 支持?

我应该做相反的事情吗?

if(type window.XDomainRequest === 'object') {
    // Assume the browser does not support XMLHttpRequest with CORS
}

最佳答案

我用这个,1表示XMLHttpRequest支持cors,2表示通过XDomainRequest支持cors,0表示不支持CORS

var  corsSupportLevel = null;    
function supportsCorsInternal() {

                if (corsSupportLevel !== null) return corsSupportLevel;

                var xhr = new XMLHttpRequest();
                if (typeof xhr.withCredentials !== 'undefined') {
                    // Supports CORS
                    corsSupportLevel = 1;
                } else if (typeof XDomainRequest !== "undefined") {
                    // IE
                    corsSupportLevel = 2;
                } else {
                    corsSupportLevel = 0;
                }
                return corsSupportLevel;
            }

关于javascript - 检测浏览器是否不支持具有完整 CORS 支持的 XMLHttpRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39279712/

相关文章:

javascript - highchart 和 .clone() 的问题

c# - 用于清除文本区域/文本框内容的按钮

windows - XP、Vista、Win7 之间 IE8 行为的差异?

javascript - 在 IE 8 中单击标签时触发单击输入

javascript - CORS - 状态 200 但 Chrome devtools 控制台中出现错误

javascript - 灯箱图片最大高度 = 用户屏幕高度

html - 当文本对齐设置为左时,为什么 ie8 会将文本居中?

javascript - 如何将 $sce.trustAsResourceUrl 与 $http 服务一起使用

ajax - 如何将 Ajax 错误结果与原始请求关联起来?

javascript - SMB-获取 Nodejs getaddrinfo ENOTFOUND