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

239
Visualizações
My Javascript JSON code in HTML page does not render the RESTful API array objects, although I get the data from the API. What should I do?

enter image description hereThe problem is that the objects of the API are not being rendered in HTML, what did I do wrong?

       <button onclick = "showCountries()">Show Countries</button>
        <div id = "feed"></div>
        <script>
            function showCountries(){
                let xhr = new XMLHttpRequest()
                    xhr.open('GET', 'https://restcountries.com/v3.1/all', true)
                    xhr.onload = function(){
                    if(xhr.status == 200){
                    console.log('success')
                    let countries = JSON.parse(this.response)
                    countries.forEach(country=>{
                        const countryCard = document.createElement('div')
                        const countryCardImage = document.createElement('img')
                        countryCard.innerHTML = country.name
                        countryCardImage.src = country.flag
                        document.getElementById('feed').appendChild(countryCard)
                    })
                }
            }
            xhr.send()
       } 
    </script> 
      
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I finally figured out how to display flags, here is the source:

                function showCountries(){
                let xhr = new XMLHttpRequest()
                    xhr.open('GET', 'https://restcountries.com/v3.1/all', true)
                    xhr.onload = function(){
                    if(xhr.status == 200){
                    console.log('success')
                    let countries = JSON.parse(this.response)
                    countries.forEach(country=>{
                        const countryCard = document.createElement('div')
                        const countryCardImage = document.createElement('div')// must be div to display small image
                        countryCard.innerHTML = country.name.common

                        countryCardImage.innerHTML = country.flag
                        console.log(country.flag)

                        document.getElementById('feed').appendChild(countryCard)
                        document.getElementById('feed').appendChild(countryCardImage) //this is it!
                    })
                }
            }
about 4 years ago · Juan Pablo Isaza Relatório

0

Because country.name is an object.You should use an string to set the innerHTML of element. According to your response data structure, you can use it like this:

function showCountries() {
  let xhr = new XMLHttpRequest()
  xhr.open('GET', 'https://restcountries.com/v3.1/all', true)
  xhr.onload = function() {
    if (xhr.status == 200) {
      console.log('success')
      let countries = JSON.parse(this.response)
      countries.forEach(country => {
        const countryCard = document.createElement('div')
        const countryCardImage = document.createElement('img')
        countryCard.innerHTML = country.name.common
        countryCardImage.src = country.flags.png
        countryCardImage.style.width = "30px"
        countryCard.append(countryCardImage)
        document.getElementById('feed').appendChild(countryCard)
      })
    }
  }
  xhr.send()
}
<button onclick="showCountries()">Show Countries</button>
<div id="feed"></div>

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