Search code examples
mapsgeocodinghere-apigeocode

Here Maps Geocode API leads to Cross-Origin error


I use the first example here in order to get the geo coordinates based on an address: https://developer.here.com/documentation/maps/3.1.15.1/dev_guide/topics/geocoding.html

My JavaScript coding looks almost the same as in the official documentation:

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

However, when the geocode request is sent, I get the following error:

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.

The API key is sent correctly, why do I still get the CORS error? If I enter the request URL manually in the browser, I get a response and everything is fine.


Solution

  • If you are testing this code by opening the file in the browser, you may get this error. I suggest using a local server for testing.