CORS 请求不适用于谷歌路线 API [英] CORS request is not working on google directions API
本文介绍了CORS 请求不适用于谷歌路线 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每次我尝试从路线 API 获取数据时,我都会收到请求的资源上不存在Access-Control-Allow-Origin"标头.我已经尝试对地理编码 API 执行相同的请求并且它有效.这是我正在使用的代码:
var xhr = new XMLHttpRequest();xhr.open('GET', 'https://maps.googleapis.com/maps/api/directions/json?origin='+encodeURIComponent(data.origin)+'&destination='+encodeURIComponent(data.destination))+'&key='+encodeURIComponent(data.key), true);xhr.send();
解决方案
您无法使用 ajax 请求访问路线 API,因为此 API 不支持 CORS.但是,您可以使用该库访问 Direction API 数据.
这里的示例取自 Google Maps API 示例: