我正在尝试对 googleMapsClient 地理编码进行 api 调用,但仅在使用 Firefox 时才收到 XHR 错误。它在 Chrome 中工作正常,但在 Firefox 中,我收到以下错误
> Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at
> https://maps.googleapis.com/maps/api/geocode/json?address=Kothaguda%2C%20Hyderabad%2C%20Telangana%2C%20India&key=xxxx.
> (Reason: missing token ‘user-agent’ in CORS header
> ‘Access-Control-Allow-Headers’ from CORS preflight channel)
不知道为什么这是 Firefox 特有的。我可以看到请求中有一个“user-agent”http header 。非常感谢对此的任何帮助。
最佳答案
您使用的是 @google/maps
软件包吗?请注意,这个包应该只与node.js一起使用,而不是在浏览器中使用。 README中也有说明。 :
Attention! This library is designed for server-side Node.js applications. Attempting to use it client-side, in either the browser or any other environment like React Native, may in some cases work, but mostly will not. Please refrain from reporting issues with these environments when attempting to use them, since server-side Node.js applications is the only supported environment for this library. For other environments, try the Maps JavaScript API, which contains a comparable feauture set, and is explicitly intended for use with client-side JavaScript.
我建议使用 Google Maps JavaScript API。
关于javascript - 特定于 Firefox - CORS 预检 channel 中的 CORS header ‘user-agent’ 中缺少 token ‘Access-Control-Allow-Headers’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53413174/