简体   繁体   English

Winui 3 中的 WebView2:从本地文件系统加载 Ionic/Angular 应用程序时出现 CORS 错误

[英]WebView2 in Winui 3: CORS errors when loading an Ionic/Angular application from the local files system

Following on from my last post , am looking for a way to wrap my Ionic/Angular application with some sort of Windows application.继我一篇文章之后,我正在寻找一种方法来使用某种 Windows 应用程序来包装我的 Ionic/Angular 应用程序。 I am looking at Electron (and having issues), but also investigating if I just created my own WinUI3 application and used Webview2.我正在查看 Electron(并且有问题),但也在调查我是否刚刚创建了自己的 WinUI3 应用程序并使用了 Webview2。

The relevant code here is这里的相关代码是

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();

When I run it, in dev tools, I get the following CORS errors when it is trying to load the js files within the index.html当我运行它时,在开发工具中,当它尝试加载 index.html 中的js文件时,出现以下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

Is there some way I can get around this?有什么办法可以解决这个问题吗?

Thanks in advance提前致谢

Looks like the solution to my other issue also solved this one.看起来我的另一个问题的解决方案也解决了这个问题。

Need to use SetVirtualHostNameToFolderMapping需要使用SetVirtualHostNameToFolderMapping

 await MyWebView.EnsureCoreWebView2Async();

 MyWebView.CoreWebView2.SetVirtualHostNameToFolderMapping(
     "appassets", "assets", CoreWebView2HostResourceAccessKind.Allow);
    
 MyWebView.Source = new Uri("http://appassets/www/index.html");
 MyWebView.CoreWebView2.OpenDevToolsWindow();

This now found the index.html and also loaded it with no CORS issues.这现在找到了 index.html 并且加载它也没有 CORS 问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM