简体   繁体   English

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

[英]CORS error when using await import() with Google API

Why I get a CORS error:为什么我收到 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.

When I try to import dynamically Google Maps API in my JavaScript :当我尝试在我的JavaScript中动态导入谷歌地图 API 时:

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

But not when I do it in my HTML :但不是当我在HTML中这样做时:

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

Any idea would be really appreciate it.任何想法都会非常感激。

Note : if I import other URL libraries I don't have problems, ex: hammer.js cdn注意:如果我导入其他 URL 库我没有问题,例如:hammer.js cdn

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

I found asolution.我找到了解决办法。 But I am not sure if this is working everywhere.但我不确定这是否适用于任何地方。

At least it is working on current Chrome, Firefox and Edge Browser.至少它适用于当前的 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 }));

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

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