嗨,我的主要应用程序 url 是http:localhost:4000
。它在内部渲染 iframe 应用程序http:localhost:4000/contentApp/index.html
(sandbox
参数为 as sandbox="allow-scripts allow-popups allow-modals allow-forms allowdownloads allow-same-origin"
)及其在 React 应用程序中。
PrintJS package
, 使用 1.0 版本并调用printJS(URL)
//URL,blob:http:localhost:4000/[guid value]
printjs
它在库中的以下代码段出现错误。
打印.js:
try{
iframeElement.focus(); // iframeElement = iframe#printJS { src: blob:http://localhost:4000/a33334343-33434-343434-343434adf', src: '', name: '', sandbox: DOMTockenList(0..)
...
// other browsers
iframeElement.contentWindow.print(); // throw CORS error at here.
} catch(error){ params.onError(error);}
finally {
(o,_function.cleanup)(params);
}
}
上面,iframeElement.contentWindow.print() 得到一个错误,并出现以下错误 CORS:
Uncaught DOMException: Blocked a frame with origin "http://localhost:4000" frame accessing a cross-origin frame.
at performPrint (webpack-internal:///./node_modules/print-js/dist/print.js:898:35)
at HTMLIframeElement.iframeElement.onload (webpack-internal:///./node_modules/print-js/dist/print.js:852:11)
..
当我删除sandbox
然后它工作。但是,我不能删除sandbox
它的应用程序结构。
有什么办法可以解决这个 CORS 问题。就像在“iframe 沙盒属性”中添加一些参数或者在“printjs”中添加任何选项来解决这个问题?感谢您的帮助。
PrintJS,内部调用iframeElement.contentWindow.print();
,我无法控制它postMessage
作为它的 printJS 包部分。不过,我可以控制 localhost:4000/contentApp ,沙盒属性。
(此外,它看起来像是在 "http://localhost:4000" != "BLOB:http://localhost:4000/GUIDvalue" 处创建的问题,(看起来很奇怪,因为它们都指向同一个来源”。)