带有 Google Direction API 的 Access-Control-Allow-Origin 不允许 Origin url [英] Origin url is not allowed by Access-Control-Allow-Origin with Google Direction API
问题描述
我知道这个问题已经有人问过并得到了回答,但对我没有帮助.
I know this question has already been asked and answered, but it didn't help me.
这是我的 jQuery 代码:
Here is my jQuery code:
var gmapsurl = 'http://maps.googleapis.com/maps/api/distancematrix/json?'+
'origins='+addr_origin+
'&destinations='+addr_destination+
'&mode=driving&language=hu&units=metric'+
'&key='+mykey+
'&sensor=false';
$.getJSON(gmapsurl, function(data) {
alert( 'OK' );
});
现在我在浏览器中收到 Origin (my site url) is not allowed by Access-Control-Allow-Origin.
错误消息.但是如果我直接把这个url写到浏览器中,那么我就得到了一个JSON结构.
Now I get Origin (my site url) is not allowed by Access-Control-Allow-Origin.
error message in browser. But if I write this url directly into the browser, then I get a JSON structure.
我该如何解决这个问题?
How can I fix this?
推荐答案
您应该使用 Google Maps API 进行地理编码:
You should use the Google Maps API for geocoding:
var geocoder = new google.maps.Geocoder();
var geocoderRequest = { address: "MountainView, CA" };
geocoder.geocode(geocoderRequest, function(results, status){
//do your result related activities here, maybe push the coordinates to the backend for later use, etc.
});
而不是通过 JSON 调用服务.当然,您应该将其包含在脚本中以使用 Google Maps API:
Instead of calling the service via JSON. You should, of course have this included in your scripts to use Google Maps API:
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
这篇关于带有 Google Direction API 的 Access-Control-Allow-Origin 不允许 Origin url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!