4

I have an issue trying to use javascript to get the circulating supply by requesting data from Tzscan API:

HTML code to request the circulating supply:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <h1>There Tezos Circulating : </h1>
  <div id="getapi"></div>
</body>
</html> 

Javascript Code to get the data from the API:

<script>
  function createNode(element) {
    return document.createElement(element);
  }

  function append(parent, el) {
    return parent.appendChild(el);
  }

  const ul = document.getElementById('getapi');
  const url = 'http://api6.tzscan.io/v3/supply';
  fetch(url)
    .then((resp) => resp.json())
    .then(function(data) {
      let span = createNode('span');
      span.innerHTML = `${data.circulating_supply/1000000}`;
      append(ul, span);
    })
    .catch(function(error) {
      console.log(error);
    }); 
 </script>

The problem is that somehow it works when I create a HTML file locally but not online.

  • it'll help to also include the errors you are getting. I know we could probably look at your codepen but for the sake of the question, it's best include the errors in the question. – Frank Mar 25 at 3:20
7

You've encountered a mixed content error. Requests can't be a mixture of http and https, they need to be all of the same, preferably https. Just change the const url = 'http://api6.tzscan.io/v3/supply'; to be https

const url = 'https://api6.tzscan.io/v3/supply';

fetch(url)
.then(resp => resp.json())
.then(({dls}) => {
    let span = createNode('span');
    span.innerHTML = dls;
    append(ul, span);
})
.catch(console.log);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.