Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么赢了';Javascript代码是否运行?_Javascript_Json - Fatal编程技术网

为什么赢了';Javascript代码是否运行?

为什么赢了';Javascript代码是否运行?,javascript,json,Javascript,Json,我是javascript新手。这是我第一次真正尝试做一些事情,除了运行hello world。我正在尝试从url检索信息并显示它。我检查了url,它返回json。“Hello World”代码会运行,但只要我在脚本标记中添加检索json的代码,什么都不会发生。我没有语法错误 <!DOCTYPE HTML> <html> <body> <p>Header...</p> <script> document

我是javascript新手。这是我第一次真正尝试做一些事情,除了运行hello world。我正在尝试从url检索信息并显示它。我检查了url,它返回json。“Hello World”代码会运行,但只要我在脚本标记中添加检索json的代码,什么都不会发生。我没有语法错误

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

标题

写下(“你好世界”); var getJSON=函数(url,回调){ var xhr=new XMLHttpRequest(); xhr.open(“get”,url,true); xhr.responseType=“json”; xhr.onload=函数(){ var status=xhr.status; 如果(状态==200){ 回调(null,xhr.response); }否则{ 回调(状态); } }; xhr.send(); }; getJSON(“http://api.tvmaze.com/schedule?country=US&date=2016-08-31", 功能(错误、数据){ if(err!=null){ 警惕(“出了问题:+err”); }否则{ 警报(“您的查询计数:+data.query.count”); } }); …页脚


我检查了您的html和js,快速查看控制台后发现问题出在这一行

alert("Your query count: " + data.query.count);
将“data.query.count”更改为“data.length”

调试代码是发现错误的最简单方法之一。 基本上,你可以在任何浏览器中点击F12来查看幕后情况


我检查了您的html和js,快速查看控制台后发现问题出在这一行

alert("Your query count: " + data.query.count);
将“data.query.count”更改为“data.length”

调试代码是发现错误的最简单方法之一。 基本上,你可以在任何浏览器中点击F12来查看幕后情况


如果检查浏览器上的控制台,您将看到代码抛出了
未捕获类型错误:无法读取未定义的属性“count”

这是因为您试图通过
data.query.count
访问响应数组的长度,而实际上它应该是
data.length

所以工作代码应该是这样的

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

标题

写下(“你好世界”); var getJSON=函数(url,回调){ var xhr=new XMLHttpRequest(); xhr.open(“get”,url,true); xhr.responseType=“json”; xhr.onload=函数(){ var status=xhr.status; 如果(状态==200){ 回调(null,xhr.response); }否则{ 回调(状态); } }; xhr.send(); }; getJSON(“http://api.tvmaze.com/schedule?country=US&date=2016-08-31", 功能(错误、数据){ if(err!=null){ 警惕(“出了问题:+err”); }否则{ 警报(“您的查询计数:+data.length”); } }); …页脚


如果检查浏览器上的控制台,您将看到代码抛出了
未捕获类型错误:无法读取未定义的属性“count”

这是因为您试图通过
data.query.count
访问响应数组的长度,而实际上它应该是
data.length

所以工作代码应该是这样的

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

标题

写下(“你好世界”); var getJSON=函数(url,回调){ var xhr=new XMLHttpRequest(); xhr.open(“get”,url,true); xhr.responseType=“json”; xhr.onload=函数(){ var status=xhr.status; 如果(状态==200){ 回调(null,xhr.response); }否则{ 回调(状态); } }; xhr.send(); }; getJSON(“http://api.tvmaze.com/schedule?country=US&date=2016-08-31", 功能(错误、数据){ if(err!=null){ 警惕(“出了问题:+err”); }否则{ 警报(“您的查询计数:+data.length”); } }); …页脚


我使用的js在线编辑器让我感到困惑。查看答案后,修复警报(“您的查询计数:”+data.query.count)语句并以HTML格式运行,即可获得结果。谢谢。我使用的是js在线编辑器,这让我感到困惑。查看答案后,修复警报(“您的查询计数:”+data.query.count)语句并以HTML格式运行,即可获得结果。非常感谢。