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

255
Visualizações
How to fetch data from api?

I want to fetch data (particularly market cap)from api and display it inside my div. But my html diplays no data on execution. What could I be doing wrong?

<text id="result"></text>

  <script>
  // API for get requests 

  let fetchRes = fetch(
        "https://api.lunarcrush.com/v2?data=assets&key=n8dyddsipg5611qg6bst9&symbol=AVAX"); 

   // fetchRes is the promise to resolve 

   // it by using.then() method 

   fetchRes.then((res) => res.json())
    .then((result) => {
      console.log(result);
      document.getElementById('result').innerHTML = result.config.data.0.market_cap;
    })
    .catch(error => {
      console.log(error);
    })
      
    </script>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Two suggestions:

  1. Why not just chain the .then() directly to the fetch()?
  2. You seem to have a bit of confusion on how to access the data in your structure - what you're after is result.data[0].market_cap.

// API for get requests 

let fetchRes = fetch("https://api.lunarcrush.com/v2?data=assets&key=n8dyddsipg5611qg6bst9&symbol=AVAX")
  .then((res) => res.json())
  .then((result) => {
    console.log(result);
    document.getElementById('result').innerHTML = result.data[0].market_cap;
  })
  .catch(error => {
    console.log(error);
  })
<text id="result"></text>

Aside: you should probably invalidate your API key that you've included here, as it's now out in public and can be used to forge requests as you to this API.

about 4 years ago · Juan Pablo Isaza Relatório

0

I am using jQuery Framework to do this easily. Check the code below.

<script>
    $.get(
        "https://api.lunarcrush.com/v2",
        {
            data: "assets",
            key: "n8dyddsipg5611qg6bst9",
            symbol: "AVAX"
        },
        function (result){
            data = JSON.parse(result);
        }
    );
</script>

You can use jQuery by adding the following code in your <head> tag.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
about 4 years ago · Juan Pablo Isaza Relatório

0

Use result.config.data[0].market_cap; instead of result.config.data.0.market_cap;

let fetchRes = fetch( 
"https://api.lunarcrush.com/v2?data=assets&key=n8dyddsipg5611qg6bst9&symbol=AVAX"); 

  

        // fetchRes is the promise to resolve

        // it by using.then() method

        fetchRes.then((res) => res.json())
    .then((result) => {
        console.log(result);
        document.getElementById('result').innerHTML = result.config.data[0].market_cap;
    })
    .catch(error => {
        console.log(error);
    });
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