【问题标题】:Detect if browser does not support XMLHttpRequest with full CORS support检测浏览器是否不支持具有完整 CORS 支持的 XMLHttpRequest
【发布时间】:2016-09-01 19:44:03
【问题描述】:

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

【问题讨论】:

    标签: javascript internet-explorer-8 xmlhttprequest xdomainrequest


    【解决方案1】:

    我用这个,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;
                }
    

    【讨论】:

      猜你喜欢
      • 2014-10-05
      • 2011-12-12
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多