我一直在尝试将json和ajax与jquery一起使用,而且我遇到了一些麻烦。我试图从json文件中获取数据以显示在我的页面上。
目前我只是想将它发送到控制台,但我在控制台中变为null。我不确定我做得对,我做错了所以我只是想知道我是否能得到一些指示。
这就是我对请求的要求
$(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的控制台响应为空。我无法看到我的正确与错误。该请求是我从这里的帖子(load json into variable)
获得的一个片段任何指针都会非常感激
提前致谢
答案 0 :(得分:4)
您的文件不是JSON!
以var refs = ....
超越作业(和分号)。
(如果你真的很懒,从@ MikeB的答案中复制/粘贴文件中的内容)
答案 1 :(得分:2)
我注意到的一件事是你的JSON无效。
第39行"size": "3.78MB",
第44行"size": "1.76MB",
第79行"size": "223KB",
所有人都有一个额外的逗号
尝试将此作为您的JSON
{
"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"
}
]
}
答案 2 :(得分:1)
在分配数据之前,您将返回Json var。请测试此更改
var json= null;
$(document).ready(function() {
$.ajax({
'async': false,
'global': false,
'url': 'js/refs.json',
'dataType': "json",
'success': function (refs) {
json= refs;
LoadedJSON();
}
});
});
function LoadedJSON(){
console.log(json);
}
答案 3 :(得分:0)