繼我上一篇博文之后,我正在尋找一種方法來用某種 Windows 應用程式包裝我的 Ionic/Angular 應用程式。我正在查看 Electron(并且有問題),但也在調查我是否剛剛創建了自己的 WinUI3 應用程式并使用了 Webview2。
這里的相關代碼是
await MyWebView.EnsureCoreWebView2Async();
MyWebView.CoreWebView2.Navigate("file:///D:/0/www/index.html"); // test while waiting how to load from Assets
//MyWebView.CoreWebView2.Navigate("ms-appx-web:///www/index.html");
MyWebView.CoreWebView2.OpenDevToolsWindow();
當我運行它時,在開發工具中,當它嘗試加載js檔案中的檔案時,我收到以下 CORS 錯誤index.html
Access to script at 'file:///D:/0/www/runtime.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, edge, https, chrome-untrusted.
runtime.js:1
Failed to load resource: net::ERR_FAILED
index.html:1
Access to script at 'file:///D:/0/www/polyfills.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, edge, https, chrome-untrusted.
polyfills.js:1
Failed to load resource: net::ERR_FAILED
index.html:1
Access to script at 'file:///D:/0/www/vendor.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, edge, https, chrome-untrusted.
vendor.js:1
Failed to load resource: net::ERR_FAILED
index.html:1
Access to script at 'file:///D:/0/www/main.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, edge, https, chrome-untrusted.
main.js:1
Failed to load resource: net::ERR_FAILED
有什么辦法可以解決這個問題嗎?
uj5u.com熱心網友回復:
看起來我的另一個問題的解決方案也解決了這個問題。
需要使用 SetVirtualHostNameToFolderMapping
await MyWebView.EnsureCoreWebView2Async();
MyWebView.CoreWebView2.SetVirtualHostNameToFolderMapping(
"appassets", "assets", CoreWebView2HostResourceAccessKind.Allow);
MyWebView.Source = new Uri("http://appassets/www/index.html");
MyWebView.CoreWebView2.OpenDevToolsWindow();
現在找到了 index.html 并加載了它,沒有 CORS 問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/400829.html
上一篇:組合,全排列以及子集超詳細講解
