嵌入式音频链接到javascript启用cors

时间:2019-05-19 10:44:32

标签: javascript audio cors cross-origin-read-blocking

我尝试将音频链接嵌入到javascript,但是被CORS政策阻止。我试图通过访问XMLHttpRequest启用它,但仍然失败。我该如何解决?还是对此问题有其他解决方案?

const createCORSRequest = (method, url) => {
            let xhr = new XMLHttpRequest();
            if ('withCredentials' in xhr) {
                xhr.open(method, url, true);
            } else if (typeof XDomainRequest != 'undefined') {
                xhr = new XDomainRequest();
                xhr.open(method, url);
            } else {
                xhr = null;
            }
            return xhr;
        }

        const request = createCORSRequest('GET',
            'https://drive.google.com/file/d/1iC99L1hYn4LwRs9XsqGOY8yLuVaATyNj/preview');
        if (!request) {
            throw new Error('CORS is not supported');
        }

        request.onload = () => {
            const responseText = request.responseText;
            console.log(responseText);
        }
        request.onerror = () => {
            console.log('Error');
        }
        request.send();

0 个答案:

没有答案