electron27版本,使用loadURL页面,出现CORS policy错误解决方案。

Access to XMLHttpRequest at 'http://localhost:8008/paths...' from origin 'http://www.exmaple.com' has been blocked by CORS policy: The request client is not a secure context and the resource is in more-private address space `local`.

github issue中给出的解决方案,这里我是使用以下代码解决的这个问题。

# 这里的代码要放在ready之前执行。也就是尽早执行这段代码。
app.commandLine.appendSwitch('disable-features', 'BlockInsecurePrivateNetworkRequests,PrivateNetworkAccessSendPreflights');

如果是chrome浏览器的话,需要禁用flag,链接:chrome://flags/#block-insecure-private-network-requests

默认值是default,改为Disabled并重启浏览器。

posted @ 2024-05-28 17:25  星小梦  阅读(132)  评论(0编辑  收藏  举报