如何检查浏览器是否支持带有 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/