尝试获取服务器响应

Trying to take server response

我正在尝试创建一个简单的 Web 应用程序来在页面中显示服务器响应,但我还是个新手。

访问此页面时 https://api.minergate.com/1.0/pool/profit-rating,它会生成一个响应。如何捕获它并将其放入我的 HTML 页面?

请告诉我最简单的方法。 :D

我正在使用 XMLHttpRequest() 执行一个简单的代码。完全如图所示:

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

我编写警报只是为了测试代码。但它从来没有为我显示 "GOOD" 和 "BAD"。

这个例子会给你 GOOD/BAD 输出,你错过了 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>