尝试获取服务器响应

Trying to take server response

本文关键字:响应 服务器 获取      更新时间:2023-09-26

我正在尝试创建一个简单的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>

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

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