我正在使用 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/