Javascript 将iframe内容设置为从fetch命令返回值
我想在我的html页面上将iframe的内容设置为js中Javascript 将iframe内容设置为从fetch命令返回值,javascript,html,iframe,embed,Javascript,Html,Iframe,Embed,我想在我的html页面上将iframe的内容设置为js中fetch命令的响应。我目前正在使用此代码 window.fetch( "https://www.youtube.com/embed/SXR0D3MzpBM?autoplay=0&mute=1", { method: 'GET', accept: 'text/html', mode: 'no-cors' }) .then(data => { console.l
fetch
命令的响应。我目前正在使用此代码
window.fetch(
"https://www.youtube.com/embed/SXR0D3MzpBM?autoplay=0&mute=1", {
method: 'GET',
accept: 'text/html',
mode: 'no-cors'
})
.then(data => {
console.log(data);
var iframe = document.getElementById('video');
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(data);
iframe.contentWindow.document.close();
});
当我测试这段代码时,iframe只显示
[Object Response]
我想在iframe中看到一个嵌入式youtube视频。
数据是您需要等待的承诺对象,或者是另一个然后
.then(async (data) => {
data = await data.text();
.....
但是还有一个问题,响应头没有访问控制允许来源
,因此您无法读取响应。而模式:“无cors”
不会绕过cors。我会接受这个答案,因为它让我向解决方案迈进了一步。我最终使用youtube api完成了这项工作。