gpt4 book ai didi

javascript - 面临 CORS 错误

转载 作者:行者123 更新时间:2023-11-27 22:41:38 27 4
gpt4 key购买 nike

当我尝试从不同域下载视频时,遇到 CORS 错误。我尝试了很多方法来解决它但无法解决。下面是我的js代码。

getVideoFile = function () {
var xhr = new XMLHttpRequest(),
blob;
xhr.open("GET",myVideo, true);
xhr.responseType = "blob";
xhr.addEventListener("load", function () {
if (xhr.status === 200) {
blob = xhr.response;
putVideoInDb(blob);
window.alert("Video file downloaded");
}
else {
window.alert("Unable to download video");
}
}, false);
xhr.send();
};

putVideoInDb = function (blob) {
var transaction = db.transaction(["Videos"], "readwrite");
var store = transaction.objectStore("Videos");
var vid = {
videoName:videoName,
video:blob,
}
var request = store.add(vid);
request.onerror = function(e) {
console.log("Error",e.target.error.name);
}
request.onsuccess = function(e) {
console.log("Done!!");
}
};

最佳答案

xhr.setRequestHeader('Access-Control-Allow-Headers', '*');

在发送请求之前设置此 header 值。欲了解更多信息,您可以在这里阅读JavaScript - XMLHttpRequest, Access-Control-Allow-Origin errors

关于javascript - 面临 CORS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38713924/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com