0

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.

  • You'll need to enable CORS, see this [post](http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource) for more information. This is assuming that the site you are trying to make the request is not your site but someone else's. – parrott-kevin Apr 22 '16 at 20:42
  • 1
    You forgot to call xhr.send(); This will actually start your request. – Mathijs Apr 22 '16 at 20:42

1 Answers1

0

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>
Mathijs
  • 373
  • 2
  • 10