Javascript Examples

Fetch #

var api_key = '<YOUR API KEY>'
var url = `https://api.geocode.earth/v1/autocomplete?api_key=${api_key}&text=london`

var options = {
  method: 'GET',
  headers: {
    'Accept': 'application/json'
  }
}

fetch(url, options)
  .then(res => console.log('success:', res))
  .catch(error => console.error('error:', error))

XMLHttpRequest #

var api_key = '<YOUR API KEY>'
var url = `https://api.geocode.earth/v1/autocomplete?api_key=${api_key}&text=london`

var xhr = new XMLHttpRequest()
xhr.open('GET', url, true)
xhr.onload = () => console.log("success", xhr.response)
xhr.onerror = () => console.error("error", xhr.response)

xhr.setRequestHeader('Accept', 'application/json')
xhr.send()