Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

251
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda