Json 谷歌地图api,获取未通过完整链接的方向,并且没有“访问控制允许来源”

Json 谷歌地图api,获取未通过完整链接的方向,并且没有“访问控制允许来源”,json,google-maps,google-maps-api-3,Json,Google Maps,Google Maps Api 3,所以我有一个地图,当用户点击任何标记时,我想得到一个json。所有变量都有一个值,当我手动将它们放入浏览器时,会得到一个响应。目前我得到: 无法加载XMLHttpRequest . 请求的服务器上不存在“Access Control Allow Origin”标头 资源因此,不允许使用来源“当然是我的url” 进入 链接如下所示: 虽然它应该是: 代码: 为了避免JavaScript代码中的CORS头问题,应该使用Maps JavaScript API的内置方向服务 将AJAX调用替换为类似于

所以我有一个地图,当用户点击任何标记时,我想得到一个json。所有变量都有一个值,当我手动将它们放入浏览器时,会得到一个响应。目前我得到:

无法加载XMLHttpRequest . 请求的服务器上不存在“Access Control Allow Origin”标头 资源因此,不允许使用来源“当然是我的url” 进入

链接如下所示:

虽然它应该是:

代码:


为了避免JavaScript代码中的CORS头问题,应该使用Maps JavaScript API的内置方向服务

将AJAX调用替换为类似于

directionsService.route({
    origin: new google.maps.LatLng(lat,lng),
    destination: new google.maps.LatLng(destinationLat,destinationLng),
    travelMode: 'DRIVING'
}, function(response, status) {
    if (status === 'OK') {
        //Process response here
    } else {
        window.alert('Directions request failed due to ' + status);
    }
}); 
看一看例子


我希望这有帮助

错误在这一行:

var test=$.getJSON'https://maps.googleapis.com/maps/api/directions/json?origin=“+lat+,+lng+”&目的地=“+destinationLat+,+destinationLng+”&键=AIzaSyAirYgs4Xnt9QabG9v56jsIcCNfNZazq65',函数数据{ 这应该是:

var test=$.getJSON'https://maps.googleapis.com/maps/api/directions/json?origin=“+lat+”、“+lng+”&目的地=“+destinationLat+”、“+destinationLat+”&键=AIzaSyAirYgs4Xnt9QabG9v56jsIcCNfNZazq65',函数数据{
请不要在那里张贴你的API密钥;
directionsService.route({
    origin: new google.maps.LatLng(lat,lng),
    destination: new google.maps.LatLng(destinationLat,destinationLng),
    travelMode: 'DRIVING'
}, function(response, status) {
    if (status === 'OK') {
        //Process response here
    } else {
        window.alert('Directions request failed due to ' + status);
    }
});