未捕获的TypeError:无法在XMLHttpRequest.xhttp.onreadystatechange上将属性'innerHTML'设置为null

时间:2019-08-27 21:03:22

标签: javascript json

未捕获的TypeError:无法在XMLHttpRequest.xhttp.onreadystatechange错误处将null的属性“ innerHTML”设置为null,也许您可​​以找到解决方法?

 <head>
 <meta charset="utf-8">
 <title></title>
  </head>
 <body>
   <script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var response = JSON.parse(xhttp.responseText);
var times = response.times;
var output = '';
for (var i = 0; i < times.length; i++) {
output += '<li>'+times[i].diena+'</li>';
  }
  document.getElementById('times').innerHTML = output;
  }
 };
   xhttp.open("GET", "time.json", true);
   xhttp.send();
  </script>
 </body>
</html>

1 个答案:

答案 0 :(得分:-1)

在Body标记内创建<div id="times"></div>,然后测试您的代码。