Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
Unable to get JSON data from Nominatim's API

My goal is to obtain the longitude, latitude and name of every country and territory in the world, with the help of Nominatim's API, using JavaScript, in order to add them as tiny and clickable points in a Leaflet world map.

Accessing the url using a browser works fine:

https://nominatim.openstreetmap.org/search?country=SG&format=json

It appears that I am unable to receive any of the console.log statements, even with one country:

var XHR = new XMLHttpRequest();
XHR.onreadystatechange = function () {
    if (XHR.readyState == 4 && XHR.status == 200) {
        console.log(XHR.responseText);
    }
};
XHR.open("GET", "https://www.nominatim.openstreetmap.org/search?country=SG&format=json");
XHR.send();
console.log(XHR);

So, I tried a different method, which failed:

let url = 'https://www.nominatim.openstreetmap.org/search?country=SG&format=json';
fetch(url)
    .then(response => response.json())
    .then(function (data) {
        latitude = data.lat;
        longitude = data.lon;
    });
console.log(latitude);
console.log(longtitude);

Please guide me on where I went wrong or what other API I can use to achieve my aforementioned target. Thank you.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Lose the www at the beginning of your URL, nominatim is a subdomain of openstreetmap. This is usually done by the web browser automatically, that's why it doesn't happen when you enter the URL in the browser

Working code:

var XHR = new XMLHttpRequest();
XHR.onreadystatechange = function () {
    if (XHR.readyState == 4 && XHR.status == 200) {
        console.log(XHR.responseText);
    }
};
XHR.open("GET", "https://nominatim.openstreetmap.org/search?country=SG&format=json");
XHR.send();
console.log(XHR);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!