简体   繁体   English

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

[英]Detect if browser does not support XMLHttpRequest with full CORS support

How do I check if a browser supports the native JavaScript XMLHttpRequest object with CORS? 如何检查浏览器是否支持带有CORS的本地JavaScript XMLHttpRequest对象? I am testing in IE 8 which as I understood only supports XDomainRequest , however doing the following in IE 8: 我正在IE 8中进行测试,据我了解,它仅支持XDomainRequest ,但是在IE 8中执行以下操作:

typeof window.XMLHttpRequest
"object"

I would have thought that would be undefined . 我以为那是undefined Basically how can I detect if a browser supports the full XMLHttpRequest with CORS support? 基本上,我如何检测浏览器是否支持带有CORS支持的完整XMLHttpRequest

Should I do the inverse? 我应该做逆运算吗?

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

I use this, 1 means XMLHttpRequest support cors, 2 means cors is supported via XDomainRequest, 0 means no CORS support 我用这个,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;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM