如何对Google Maps API进行跨域AJAX调用?
我正在尝试对 $.getJSON 调用“noreferrer">Google 地图地理编码网络服务,但由于跨域安全问题,这不起作用。
我无法在网上弄清楚,但我读过一些有关 Google Javascript API 或 JSONP 的内容,但到目前为止还没有明确的答案......
有人能启发我吗?
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为使用服务器端地理编码网络服务 当 Google 地图提供全功能客户端地理编码 API 对于 JavaScript。
首先,这会自动解决您的同源问题,此外,请求限制将按客户端 IP 地址计算,而不是按服务器 IP 地址计算,这对于受欢迎的网站来说可能会产生巨大的差异。
这是一个使用 JavaScript Geocoding API v3 的非常简单的示例:
如果由于某种原因您仍然想使用服务器端 Web 服务,您可以设置一个非常简单的 反向代理,可能使用 mod_proxy 如果您使用的是 Apache。这将允许您对 AJAX 请求使用相对路径,而 HTTP 服务器将充当任何“远程”位置的代理。
在 mod_proxy 中设置反向代理的基本配置指令是 ProxyPass。您通常会按如下方式使用它:
在这种情况下,浏览器可以向
/geocode/output?parameters
发出请求,但服务器将通过充当http:// 的代理来提供此请求。 /maps.google.com/maps/api/geocode/output?parameters
。I can see no advantage in using the Server-side Geocoding Web Service when Google Maps provides a full featured Client-side Geocoding API for JavaScript.
First of all, this automatically solves your same-origin problem, and in addition the request limits would be calculated per client IP address instead of of per server IP address, which can make a huge difference for a popular site.
Here's a very simple example using the JavaScript Geocoding API v3:
If for some reason you still want to use the server-side web-service, you could set up a very simple reverse proxy, maybe using mod_proxy if you are using Apache. This would allow you to use relative paths for your AJAX requests, while the HTTP server would be acting as a proxy to any "remote" location.
The fundamental configuration directive to set up a reverse proxy in mod_proxy is the ProxyPass. You would typically use it as follows:
In this case, the browser could make a request to
/geocode/output?parameters
but the server would serve this by acting as a proxy tohttp://maps.google.com/maps/api/geocode/output?parameters
.