简体   繁体   English

如何检测XMLHttpRequest是否支持responseType =“arraybuffer”?

[英]How to feature detect if XMLHttpRequest supports responseType = “arraybuffer”?

I want to know if the browser supportes XMLHttpRequest.responseType = "arraybuffer" . 我想知道浏览器是否支持XMLHttpRequest.responseType = "arraybuffer" Problem is, that I can not test agains some "general" xhr2 support, since iOS 4.2 has partial xhr2 support which includes (ie) XMLHttpRequestUpload but not responseType = "arraybuffer" . 问题是,我无法再次测试一些“通用”xhr2支持,因为iOS 4.2具有部分xhr2支持,其中包括(即) XMLHttpRequestUpload但不包括responseType = "arraybuffer"

I am using the following: 我使用以下内容:

var supported = typeof new XMLHttpRequest().responseType === 'string';

In all browsers I tested that support this, the default value of responseType is an empty string (just like it says in the spec: http://www.w3.org/TR/XMLHttpRequest/#the-responsetype-attribute ), in browsers that don't support responseType the value of the attribute is undefined. 在我测试的所有支持这个的浏览器中,responseType的默认值是一个空字符串(就像它在规范中所说: http//www.w3.org/TR/XMLHttpRequest/#the-responsetype-attribute ),不支持responseType的浏览器未定义属性的值。

Checking of ArrayBuffer should be a good feature detection. 检查ArrayBuffer应该是一个很好的特征检测。

If a userAgent supports the ArrayBuffer object then it's likely it will work with XHR2 如果userAgent支持ArrayBuffer对象,那么它很可能适用于XHR2

However as noted, it would be best to do a feature test and not a feature detection. 但是,如上所述,最好进行功能测试,而不是功能检测。

function IsArrayBufferSupported(cb){
    var xhr = new XMLHttpRequest();
    xhr.open('GET', '/', true);
    try {
       xhr.responseType = "arraybuffer";
    } catch (e){
        return cb(false);
    }
    xhr.onload = function onload() {
        if (ArrayBuffer.prototype.isPrototypeOf(this.response)) {
            return cb(true);
        }
        cb(false);
    }
    xhr.send();
}

Set responseType to "arraybuffer" and check if it got the new value: responseType设置为"arraybuffer"并检查它是否获得新值:

// call like isResponseTypeSupported('arraybuffer')
function isResponseTypeSupported(responseType) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', '/');
    try {
        xhr.responseType = responseType;
    } catch (e) {
        return false;
    }
    return xhr.responseType === responseType;
}

Using Modernizr this is covered under Modernizr.xhr2 . 使用Modernizr,这在Modernizr.xhr2有所涉及。 Following up on the comments regarding partial support Modernizr.dataview might be even more accurate. 跟进有关部分支持的评论, Modernizr.dataview可能更准确。

(function(modernizr, ns){
    ns.isSupported = (function(){
        return modernizr.xhr2 && modernizr.dataview;
    });

    return ns;
}(window.Modernizr, window.NameSpace || {}));

I expect both features to be supported or not. 我希望这两个功能都得到支持。

If you just want to detect if the "arraybuffer" response is supported, just check if it's in the global object. 如果您只想检测是否支持"arraybuffer"响应,只需检查它是否在全局对象中。 If you want to detect other features just assign the XHR().responseType until the browser empty it "" or throw a error. 如果要检测其他功能,只需分配XHR().responseType直到浏览器将其清空""或抛出错误。

function isAjaxResponseSupported(type) {
    var xhr = new XMLHttpRequest;

    /* Check if .responseType is supported first */
    if (typeof xhr.responseType === 'string') {

        /* Some browsers throw error for invalid .responseType */
        try {
            xhr.responseType = type;
            // If they don't,
            // check if .responseType is equal to @type.
            return xhr.responseType === type;
        } catch (e) {
            return false;
        }

    ; else return false;        
}

Have you tried something like this? 你尝试过这样的事吗?

if(typeof(XMLHttpRequestUpload) == "undefined"){
    //not supported
}

Edit 编辑

I think you might be stuck with somthing nasty like this 我想你可能会遇到像这样令人讨厌的事情

function IsArrayBufferSupported(){
    var xhr = new XMLHttpRequest();
    xhr.open('GET', '/', true);
    try{
       xhr.responseType = "arraybuffer";
       return true;
    }catch(e){return false;}
}

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

相关问题 JavaScript XMLHttpRequest.responseType as "arraybuffer" -- 如何获取当前的数组缓冲区块 - JavaScript XMLHttpRequest.responseType as "arraybuffer" -- how to get current arraybuffer chunk javafx webview javascript XMLHttpRequest responseType数组缓冲区 - javafx webview javascript XMLHttpRequest responseType arraybuffer 用于responseType arraybuffer的xmlHttpRequest GET在电子中获得空响应 - xmlHttpRequest GET for responseType arraybuffer gets empty response in electron 如何在Javascript中将文本转换为ArrayBuffer,如ArrayBuffer的responseType - How to convert text to ArrayBuffer like responseType of ArrayBuffer in Javascript 如何检测浏览器支持此HTML5功能 - How to detect browser supports this HTML5 feature 将HTTP responseType设置为arraybuffer - Set HTTP responseType as arraybuffer axios 如何将 blob 与 arraybuffer 作为 responseType 处理? - how does axios handle blob vs arraybuffer as responseType? 如果responseType是arraybuffer,如何从$ http读取JSON错误响应 - How to read JSON error response from $http if responseType is arraybuffer new XMLHttpRequest()。responseType为空 - new XMLHttpRequest().responseType empty 如何(功能)检测浏览器是否支持WebM alpha透明度? - How to (feature) detect if browser supports WebM alpha transparency?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM