joj*_*ojo 3 html javascript iframe cors
请参阅下面的示例代码
知道如何解决这个问题吗?
const iframeElement = document.createElement("iframe");
iframeElement.src = "https://www.bing.com"
//iframeElement.setAttribute("sandbox", "allow-forms allow-modals allow-popups allow-scripts");
iframeElement.onload = (e) => {
iframeElement.contentWindow.postMessage("foo", "https://www.bing.com");
};
const containerElement = document.getElementById("place-holder-for-iframe");
containerElement.appendChild(iframeElement);
Run Code Online (Sandbox Code Playgroud)
您可以使用此 jsbin 链接http://jsbin.com/gafobulife/edit?js,output尝试一下
如果您未allow-same-origin在沙箱属性中设置,则内容将被视为来自唯一来源:请参阅https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#Attributes,以及https://www.html5rocks.com/en/tutorials/security/sandboxed-iframes/。
令人困惑的是,allow-same-origin这并不意味着 iframe 将能够访问其父级,就好像它们具有相同的来源(除非它们具有相同的来源),但这意味着它可以将其视为来自它的正常原点(在本例中为https://www.bing.com)。
所以你可以改变:
iframeElement.setAttribute("sandbox", "allow-forms allow-modals allow-popups allow-scripts")'
Run Code Online (Sandbox Code Playgroud)
到
iframeElement.setAttribute("sandbox", "allow-forms allow-modals allow-popups allow-scripts allow-same-origin");
Run Code Online (Sandbox Code Playgroud)
或者,如果您不希望 iframe 保持其原点,请更改:
iframeElement.contentWindow.postMessage("foo", "https://www.bing.com");
Run Code Online (Sandbox Code Playgroud)
到
iframeElement.contentWindow.postMessage("foo", "*");
Run Code Online (Sandbox Code Playgroud)
对我来说,如果我不使用 ,还有其他错误allow-same-origin,很可能来自bing.com配置方式。