我在这里使用第一个示例,以便根据地址获取地理坐标:https://developer.here.com/documentation/maps/3.1.15.1/dev_guide/topics/geocoding.html
我的JavaScript编码看起来与官方文档中的几乎相同:
var platform = new H.service.Platform({
'apikey': 'HERE IS MY API KEY'
});
// Get an instance of the geocoding service:
var service = platform.getSearchService();
service.geocode({
q: 'Berlin'
}, (result) => {
result.items.forEach((item) => {
console.log("test");
});
}, alert);
但是,当发送地理编码请求时,出现以下错误:
Access to XMLHttpRequest at '**https://geocode.search.hereapi.com/v1/geocode?xnlp=CL_JSMv3.1.16.1&apikey=[HERE IS MY API KEY]&q=Berlin**' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
API密钥已正确发送,为什么仍然出现CORS错误?如果我在浏览器中手动输入请求URL,则会收到响应,一切都很好。
如果通过在浏览器中打开文件来测试此代码,则可能会收到此错误。我建议使用本地服务器进行测试。