javascript - 我怎样才能摆脱这个错误以及为什么我无法进入该页面

标签 javascript jquery json

我正在使用 jquery 从这里发出 json 请求 http://quotes.rest/qod.json我不断在日志中收到此错误。这很重要吗?我怎样才能摆脱它?最后为什么我没有在页面上显示任何内容。我可以在日志上看到引用。为什么不在页面上?我错过了什么?谢谢

Uncaught ReferenceError: json is not defined
    at HTMLButtonElement.<anonymous> (VM1741 pen.js:9)
    at HTMLButtonElement.dispatch (VM1796 jquery.min.js:3)
    at HTMLButtonElement.q.handle (VM1796 jquery.min.js:3)

这是我的 jquery 代码

$(document).ready(function() {  
  /*getting random quote on button click*/
  $('#getMessage').on("click", function() {
    $.getJSON("https://quotes.rest/qod.json", function(json) {      
      console.log(json.contents.quotes[0])
      var quoteArr = json.contents.quotes[0];
      console.log(quoteArr.quote)
      console.log(quoteArr.author)
});
    $("#quote-content").html(quoteArr.quote);
    $("#quote-author").html(quoteArr.author);
  });  
});

最佳答案

工作正常:

$(document).ready(function() {  
  /*getting random quote on button click*/
  $('#getMessage').on("click", function() {
    $.getJSON("https://quotes.rest/qod.json", function(json) {      
        console.log(json.contents.quotes[0])
        var quoteArr = json.contents.quotes[0];
        console.log(quoteArr.quote)
        console.log(quoteArr.author)

        $("#quote-content").html(quoteArr.quote);
        $("#quote-author").html(quoteArr.author);
    });
  });  
});

唯一的问题是此代码块的位置:

$("#quote-content").html(quoteArr.quote);
$("#quote-author").html(quoteArr.author);

关于javascript - 我怎样才能摆脱这个错误以及为什么我无法进入该页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44706443/

相关文章:

javascript - 了解 javascript 中的回调和闭包

jquery - 使用 jQuery 进行转换不起作用

json - Laravel 5 PHPUnit 测试 json 后

javascript - 遍历 JSON 数组,显示两个然后计数

javascript - 旋转正弦曲线

javascript - 这两种在 JavaScript 中定义函数的方式有什么区别?

javascript - 警报事件有效,但我的单击更改颜色的 javascript 函数不起作用

javascript - 如何获得 % relative div 中的值

javascript - 如何使用jquery从数组中删除未选中的项目

java - Object.parse 中的 JSON 意外标记