2017-06-22 86 views
0

在得到我使用jQuery从这里http://quotes.rest/qod.json作出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); 
    }); 
}); 
+0

您的两个朝向底线('$(..)。html')应该是'$ .getJSON'回调函数中。这可能是你没有看到任何东西的原因。 –

回答

2

其做工精细:

$(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); 
+0

谢谢,为什么我一直得到相同的报价一遍又一遍? – miatech

0

我没有得到你的错误在我的试用。我已经做了代码的小修改,虽然

$(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); 
    }); 
    }); 
}); 

我希望这有助于。 我用jquery 1.9.1。 jsfiddle link