XMLHttpRequest在带有blob和arraybuffer的IOS上失败

时间:2018-08-09 20:55:32

标签: javascript jquery html html5

我正在尝试使用XMLHttpRequest下载一个大约50kb的mp3文件(IOS9不支持抓取),它总是失败,而且我似乎无法获得任何有用的错误信息:

var request = new XMLHttpRequest();
request.open("GET", files_path + '/' + test['test'][i]['sound'].trim() + '?' + token);
request.responseType = "arraybuffer";
request.onreadystatechange = function (e) {
    if (this.readyState === 4) {
        if (this.status === 200)
            console.log('XMLHttpRequest onreadystatechange: ' + this.responseText);
        else
            console.log('XMLHttpRequest onreadystatechange Error: ' + this.status + ', ' + this.statusText);
    }
};
request.onerror = (function(group_test_index, test_index, game_index_, preload, i) {
    return function(e) {
        console.log(new Date().format("dd-MM-yyyy hh:mm:ss") + ' - do_game2() XMLHttpRequest failed: ' + this.statusText);
    }
})(group_test_index, test_index, game_index_, preload, i);
request.onload = (function(group_test_index, test_index, game_index_, preload, i) {
    return function() {
        console.log(new Date().format("dd-MM-yyyy hh:mm:ss") + ' - do_game2() XMLHttpRequest succeeded');

        // https://staxmanade.com/2015/11/how-to-base64-and-save-a-binary-audio-file-to-local-storage-and-play-it-back-in-the-browser/
        var reader = new FileReader();

        reader.addEventListener("loadend", (function (group_test_index, test_index, game_index_, preload, i) {
            return function () {
                console.log(new Date().format("dd-MM-yyyy hh:mm:ss") + ' - do_game2() FileReader loadend');
            }
        })(group_test_index, test_index, game_index_, preload, i));

        reader.addEventListener("error", (function (group_test_index, test_index, game_index_, preload, i) {
            return function () {
                console.log(new Date().format("dd-MM-yyyy hh:mm:ss") + ' - do_game2() FileReader error');
            }
        })(group_test_index, test_index, game_index_, preload, i));

        var b = new Blob([new Uint8Array(this.response)], {type: "audio/mpeg"});

        reader.readAsDataURL(b);
    }
})(group_test_index, test_index, game_index_, preload, i);
request.send();

这会在Safari控制台中输出错误:0,''...在Chrome中是同样的问题。

是否有办法获取更多有用的错误信息,或者没有人有任何想法为什么这在IOS9上不起作用? (在台式机上工作正常)

谢谢!

西蒙。

0 个答案:

没有答案