显示 JSON 请求

Display JSON Request

<html>
<head>
<script>
var request = new XMLHttpRequest();
request.open('GET', 'https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCiWypivJjO5CIQPVVfFlVQA&key=AIzaSyD5FVw6fP3ingbjTvUEzm-EYctX2ytfL2Y', true);

request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
    // Success!
    var data = JSON.parse(request.responseText);
  } else {
    // We reached our target server, but it returned an error

  }
};
</script>
</head>
<body>
</body>
</html>

我的问题很简单,我已请求 JSON YouTube v3 数据 API,但是,我想显示从 link 请求的数据:https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCiWypivJjO5CIQPVVfFlVQA&key=AIzaSyD5FVw6fP3ingbjTvUEzm-EYctX2ytfL2Y 即订阅者数量,我如何才能使用此 GET 请求将订阅者数量加载到元素的 innerHTML 中?我必须做什么?

使用通过解析 JSON (data) 获得的对象,获取 subscriberCount 值并将其放入 div 或使用 [= 的其他元素13=]。确保您使用 request.send() 实际发送您的 AJAX 请求。

<html>
<head>
<script>
var request = new XMLHttpRequest();
request.open('GET', 'https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCiWypivJjO5CIQPVVfFlVQA&key=AIzaSyD5FVw6fP3ingbjTvUEzm-EYctX2ytfL2Y', true);

request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
    var data = JSON.parse(request.responseText);
    document.getElementById("subs").innerHTML = data.items[0].statistics.subscriberCount;
  } else {
    // We reached our target server, but it returned an error
  }
};

request.send();
</script>
</head>
<body>
<div id="subs"></div>
</body>
</html>