2017-06-22 55 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

相關問題