-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>

2

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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. docs.avascan.info/quickstart-graphql#api-version-0-3
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
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.