带Google方向API的Access-Control-Allow-Origin不允许源网址 [英] Origin url is not allowed by Access-Control-Allow-Origin with Google Direction API

查看:134
本文介绍了带Google方向API的Access-Control-Allow-Origin不允许源网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的jQuery代码:

  var gmapsurl ='http://maps.googleapis.com/maps/api/distancematrix/json?'+ 
'origins ='+ addr_origin +
'& destinations ='+ addr_destination +
'& mode = driving& language = hu& units = metric'+
'& key ='+ mykey +
' &安培;传感器=假';
$ .getJSON(gmapsurl,function(data){
alert('OK');
});

现在我得到 Origin(我的网站网址) Control-Allow-Origin。浏览器中的错误信息。但是如果我直接将这个url写入浏览器,那么我得到一个JSON结构。



我该如何解决这个问题?



  var geocoder =h2_lin>解决方案

= new google.maps.Geocoder();
var geocoderRequest = {address:MountainView,CA};
geocoder.geocode(geocoderRequest,function(results,status){
//在这里做你的结果相关的活动,也许把坐标推到后端以备后用,等等。

而不是通过JSON调用服务。
当然,您的脚本中应该包含这些代码,以便使用Google Maps API:

 < script src = http://maps.google.com/maps/api/js?sensor=false\"></script> 


I know this question has already been asked and answered, but it didn't help me.

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' );
});

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?

解决方案

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.
});

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方向API的Access-Control-Allow-Origin不允许源网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆