我一直在尝试将 json 和 ajax 与 jquery 一起使用,但遇到了一些麻烦。我正在尝试从 json 文件中获取数据以显示在我的页面上。
目前我只是想将它发送到控制台,但我在控制台中得到空值。我不确定我做对了什么,做错了什么,所以我只是想知道我是否可以得到一些指示。
这就是我的请求
$(document).ready(function() {
var json = (function () {
var json = null;
$.ajax({
'async': false,
'global': false,
'url': 'js/refs.json',
'dataType': "json",
'success': function (refs) {
json = refs;
}
});
return json;
})();
console.log(json);
这就是 refs.json 中的内容
var refs = {
"referee": [
{
"name": "Ellie",
"company": "University",
"position": "Lecturer",
"address": "",
"phone": "5750",
"email": "ellie@ac.uk",
"type": "Education"
},
{
"name": "Matthew",
"company": "",
"position": "",
"address": "23 High Street",
"phone": " 962",
"email": "matthew@aaa.com",
"type": "Character"
}
],
"project": [
{
"tab": "Dissertation",
"title": "Can technology in the home be used to enhance learning of numeracy, in conjunction with the nantional curriculum",
"yr": "2013",
"link": [
{
"name": "Artefact",
"links": "fypc",
"size": "",
"misc": ""
}
],
"docs": [
{"type": "doc",
"links": "fyp.docx",
"size" :"3.78MB",
},
{"type": "pdf",
"links": "fyp.pdf",
"size" :"1.76MB",
}
],
"purpose": "School - Anglia Ruskin University",
"grade": "Not yet awarded",
"sshot": "fypc.png"
},
{
"tab": "Network and IT Operations",
"title": "Virtual inter-office network with firewall. (Built using PacketTracer 5.3.3)",
"yr": "2013",
"link": [
{
"name": "Serial Cable Connection Version",
"links": "",
"size": "204KB",
"misc": "(Submitted version)"
},
{
"name": "Frame Relay Version",
"links": "",
"size": "129KB",
"misc": ""
},
{
"name": "Packet Tracer 5.3.3 Download",
"links": "",
"size": "48.2MB",
"misc": "(.zip)"
}
],
"docs": [
{
"type": "doc",
"links": "nio.docx",
"size" :"223KB",
},
{
"type": "pdf",
"links": "nio.pdf",
"size" :"943.KB",
}
],
"purpose": "School - Anglia Ruskin University",
"grade": "Not yet awarded",
"sshot": "nio1.png"
}
]
};
正如我所说,控制台使用 console.log 的响应为空。我看不出我要去哪里对或错。该请求是我从此处的帖子中获得的片段(将json 加载到变量中)
任何指针将不胜感激
提前致谢