简体   繁体   English

为什么不运行Javascript代码?

[英]Why won't Javascript code run?

I'm new new to javascript. 我是javascript新手。 This is my first real attempt to do something besides running hello world. 这是我第一次真正的尝试,除了运行hello world。 I'm attempting to retrieve information from a url and display it. 我正在尝试从URL检索信息并显示它。 I checked the url and it returns json. 我检查了网址,并返回json。 The "Hello World" code runs but as soon as I add the code that retrieves the json inside the script tags nothing happens. “ Hello World”代码运行,但是一旦我添加了在脚本标签内检索json的代码,就什么也没有发生。 I'm getting no syntax errors. 我没有语法错误。

<!DOCTYPE HTML>

<html>

<body>

  <p>Header...</p>

  <script>
    document.write("Hello World");

    var getJSON = function(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open("get", url, true);
    xhr.responseType = "json";
    xhr.onload = function() {
      var status = xhr.status;
      if (status == 200) {
        callback(null, xhr.response);
      } else {
        callback(status);
      }
    };
    xhr.send();

};

  getJSON("http://api.tvmaze.com/schedule?country=US&date=2016-08-31",
  function(err, data) {
  if (err != null) {
    alert("Something went wrong: " + err);
  } else {
    alert("Your query count: " + data.query.count);
  }
});

  </script>
  <p>...Footer</p>

</body>

</html>

I checked your html and js and one quick look into the console showed me that the problem was this line 我检查了您的html和js,然后快速查看控制台,发现问题出在这一行

alert("Your query count: " + data.query.count);

change the "data.query.count" to "data.length" 将“ data.query.count”更改为“ data.length”

alert("Your query count: " + data.length);

Debuging code is one of the easiest ways to find errors. 调试代码是发现错误的最简单方法之一。 You can hit F12 in basicly any browser to inspect the things behind the scene 基本上,您可以在任何浏览器中按F12键来检查幕后情况

在此处输入图片说明

If you examine the console on your browser you will see that your code is throwing the error of Uncaught TypeError: Cannot read property 'count' of undefined . 如果在浏览器上检查控制台,则会看到代码抛出Uncaught TypeError: Cannot read property 'count' of undefined错误Uncaught TypeError: Cannot read property 'count' of undefined

This is because you are attempting to access the length of the response array by means of data.query.count , when in fact it should be data.length . 这是因为您试图通过data.query.count访问响应数组的长度,而实际上它应该是data.length

So the working code should look like; 因此,工作代码应如下所示:

<!DOCTYPE HTML>

<html>

<body>

  <p>Header...</p>

  <script>
    document.write("Hello World");

    var getJSON = function(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open("get", url, true);
    xhr.responseType = "json";
    xhr.onload = function() {
      var status = xhr.status;
      if (status == 200) {
    callback(null, xhr.response);
      } else {
    callback(status);
      }
    };
    xhr.send();

};

  getJSON("http://api.tvmaze.com/schedule?country=US&date=2016-08-31",
  function(err, data) {
  if (err != null) {
    alert("Something went wrong: " + err);
  } else {
    alert("Your query count: " + data.length);
  }
});

  </script>
  <p>...Footer</p>

</body>

</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM