检测浏览器是否不支持具有完整 CORS 支持的 XMLHttpRequest
Detect if browser does not support XMLHttpRequest with full CORS support
如何检查浏览器是否支持带有 CORS 的本机 JavaScript XMLHttpRequest 对象?我正在 IE 8 中进行测试,据我所知,它仅支持 XDomainRequest
,但是在 IE 8 中执行以下操作:
typeof window.XMLHttpRequest
"object"
我原以为会是 undefined
。基本上我如何检测浏览器是否支持完整的 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;
}
如何检查浏览器是否支持带有 CORS 的本机 JavaScript XMLHttpRequest 对象?我正在 IE 8 中进行测试,据我所知,它仅支持 XDomainRequest
,但是在 IE 8 中执行以下操作:
typeof window.XMLHttpRequest
"object"
我原以为会是 undefined
。基本上我如何检测浏览器是否支持完整的 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;
}