[英]How to make cross-domain AJAX calls to Google Maps API?
I'm trying to make a jQuery $.getJSON
call to the Google Maps Geocoding webservice , but this doesn't work because of cross-domain security issues. 我正在尝试对Google Maps Geocoding网络服务进行jQuery
$.getJSON
调用,但这由于跨域安全性问题而无法使用。
I haven't been able to figure it out online, but I've read a bit about Google Javascript API or JSONP, but so far no clear answer... 我还没能在网上找到答案,但是我已经读了一些有关Google Javascript API或JSONP的文章,但是到目前为止还没有明确的答案...
Could anyone enlight me? 有人能启发我吗?
Thanks! 谢谢!
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. 当Google Maps为JavaScript提供全功能的客户端地理编码API时,使用服务器端地理编码Web服务没有任何优势。
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. 首先,这将自动解决您的同源问题,此外,请求限制将按客户端IP地址而不是服务器IP地址进行计算,这对于受欢迎的网站可能会产生巨大的影响。
Here's a very simple example using the JavaScript Geocoding API v3: 这是一个使用JavaScript Geocoding API v3的非常简单的示例:
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();
var address = 'London, UK';
if (geocoder) {
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results[0].geometry.location);
}
else {
console.log("Geocoding failed: " + status);
}
});
}
</script>
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. 如果出于某种原因您仍想使用服务器端Web服务,则可以设置一个非常简单的反向代理 ,如果使用的是Apache,则可以使用mod_proxy 。 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.
这将允许您为AJAX请求使用相对路径,而HTTP服务器将充当任何“远程”位置的代理。
The fundamental configuration directive to set up a reverse proxy in mod_proxy is the ProxyPass. 在mod_proxy中设置反向代理的基本配置指令是ProxyPass。 You would typically use it as follows:
您通常按以下方式使用它:
ProxyPass /geocode/ http://maps.google.com/maps/api/geocode/
In this case, the browser could make a request to /geocode/output?parameters
but the server would serve this by acting as a proxy to http://maps.google.com/maps/api/geocode/output?parameters
. 在这种情况下,浏览器可以向
/geocode/output?parameters
发出请求,但是服务器可以通过充当http://maps.google.com/maps/api/geocode/output?parameters
的代理来满足请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.