将 await import() 与 Google API 一起使用时出现 CORS 错误



为什么我收到 CORS 错误:

Access to script at 'https://maps.googleapis.com/maps/api/js' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

当我尝试在我的JavaScript中动态导入Google Maps API时:

await import('https://maps.googleapis.com/maps/api/js')

但当我在我的HTML中执行此操作时不会:

<script defer src="https://maps.googleapis.com/maps/api/js"></script>

任何想法将不胜感激。

注意:如果我导入其他 URL 库我没有问题,例如:锤子.js cdn

async function init_map() {
await import('https://maps.googleapis.com/maps/api/js')
}
init_map()

我找到了解决方案。但我不确定这是否在任何地方都有效。

至少它可以在当前的Chrome,Firefox和Edge浏览器上运行。

var url = "https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places&key=" + gkey;
$(document.head).append($('<script>', { src: url }));

最新更新