-1

Hi i want to fetch data from avascan api and display it in html, but i am not able to do this. I have tried fetch api, json and ajax ways but none worked for me. Any suggestions? This is my html https://avascan.info/api/v1/home/statistics

const api_url = 'https://avascan.info/api/v1/home/statistics';


async function getAVA() {
  const response = await fetch(api_url);
  const data = await response.json();
  const {
    blockchains,
    validators
  } = data;
  document.getElementById('lat').textContent = blockchains.toFixed(2);
  document.getElementById('lon').textContent = validators.toFixed(2);
}

getAVA();

setInterval(getAVA, 1000);
<h1>What the stats?</h1>

<p>
  blockchains: <span id="lat"></span>°<br /> validators: <span id="lon"></span>°
</p>

<div id="issMap"></div>
showdev
  • 28,454
  • 37
  • 55
  • 73
  • 1
    You are probably being hit by a CORS error. is your javascript code on the same domain as the API (https://avascan.info/api/v1/home/statistics)? – Abbas Aug 24 '21 at 18:35
  • Does this answer your question? [How does Access-Control-Allow-Origin header work?](https://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work) – showdev Aug 24 '21 at 18:42

2 Answers2

0

Looks like you have this error: No 'Access-Control-Allow-Origin' header is present on the requested resource.

This is a CORS protection and you may need certain requirements to fetch this dat such an api key, or update configuration your options in the fetch method

Here is a resource to help

0

You are getting a CORS protection error, as mentioned before.

However it seems you need to use a GraphQL API: https://graphql.avascan.info/

Look at this quick example:

      async function getAVA() {

        fetch('https://graphql.avascan.info', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
            query: `
            query {
                stats {
                    priceAvaxUsd,
                    connectedNodes
                }
            }`
            }),
            })
            .then((res) => res.json())
            .then((result) => console.log(result));
      }

      getAVA();
GoJoe
  • 54
  • 6
  • I also need total no. Of validators and blockchains to display them in html, seems like it cannot retrive them, btw how do I show this console. Log data in html? – hitesh Pahwa Aug 25 '21 at 04:15
  • It seems you will need to wait to accomplish what you need. The GraphQL API is deprecated and the new one is not yet available. https://docs.avascan.info/quickstart-graphql#api-version-0-3 – GoJoe Aug 25 '21 at 19:27
  • to show the data in html instead of console.log, you would need to move these commands `document.getElementById('lat').textContent = result.data.stats.connectedNodes.toFixed(2);` inside the last `then` – GoJoe Aug 25 '21 at 19:31