javascripthtmlwebserver-response

Trying to take server response


I'm trying to creat a simple web app that shows the server response in a page but I'm pretty newbie.

When acessing this page https://api.minergate.com/1.0/pool/profit-rating, it generates a response. How do I can capture it and put it in my HTML page?

Please tell me the simplest way. :D

I'm executing a simple code with XMLHttpRequest(). Exactly as shown:

<!DOCTYPE html>
<html>
<body>

<script>
function test() {
  var xhr = new XMLHttpRequest();
  xhr.open('GET', 'https://api.minergate.com/1.0/pool/profit-rating', true);
  xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
      alert(xhr.responseText);
      alert("GOOD");
    }
    else alert("BAD");
  };alert("EXIT");
};
</script>
<button onclick='test()'>Click</button>
</body>
</html>

I've wrote the alerts just to test the code. But it never shows the "GOOD" and "BAD" for me.


Solution

  • This example will give you the GOOD/BAD output, you missed the xhr.send();

    <!DOCTYPE html>
    <html>
    <body>
    <script>
    function test() {
      var xhr = new XMLHttpRequest();
      xhr.open('GET', 'https://api.minergate.com/1.0/pool/profit-rating', true);
      xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
          alert(xhr.responseText);
          alert("GOOD");
        }
        else alert("BAD");
      };
      xhr.send(null);
      alert("EXIT");
    };
    </script>
    <button onclick='test()'>Click</button>
    </body>
    </html>