我的json文件被稱爲data.JSON,它與我的index.html和我的js文件位於同一目錄中。
在html中我已經鏈接了jquery,然後是js文件。
在data.JSON的數據是:
[
{
"question": "What OS does the One Plus One run by default?",
"choices": ["Android 5.0 Lollipop", "Cyanogen Mod 11", "Android Kitkat", "iOS", "Cyanogen Mod 11"]
},
{
"question": "Who is the lead singer of the British band 'Muse'?",
"choices": ["Matt Bellamy", "John Lennon", "Will.I.Am", "Danny O'Donoghue", "Matt Bellamy"]
},
{
"question": "Which Japanese/American singer released 'Goodbye Happiness'?",
"choices": ["Jay Park", "Utada Hikaru", "Gackt", "Yoshika", "Utada Hikaru"]
},
{
"question": "The popular Korean girl-group known as 'Girl's Generation' is also known as:",
"choices": ["4Minute", "SNSD", "After School", "Girl\'s Day", "SNSD"]
},
{
"question": "What sub-genre of EDM does the hit 'Selfie' fall under?",
"choices": ["Melbourne Bounce", "Big House Boom", "Dubstep", "Grime", "Melbourne Bounce"]
}
]
JavaScript文件,我鏈接包含:
$(document).ready(function() {
'use strict';
var allQuestions;
$.getJSON("data.json", function (info) {
alert("hi");
});
});
我得到實際的問題是,我甚至不能似乎得到彈出警報。我想要做的就是將我的JSON中的加載數據加載到我的JS文件中,這樣我就可以運行測驗腳本,但是我遇到了只是加載JSON的麻煩。
謝謝!
您是否從Web服務器提供JSON文件?如果沒有,如果你已經安裝了python,進入你的data.json文件所在的文件夾,然後編寫'python -m SimpleHTTPServer',然後進入你的瀏覽器並指向http://127.0.0.1:8000/data.json和瞧。 – 2014-12-01 16:00:23
你可以添加類似.fail(function(jqxhr,textStatus,error){});看看具體的錯誤是什麼。 – bsayegh 2014-12-01 16:06:10
'(document).ready'應該是'$(document).ready',如果這不是一個錯字。 – Andy 2014-12-01 16:08:38