I have been trying to use json and ajax with jquery and I am running in to some trouble. I am trying to get data from a json file to display on my page.
At the moment I am just trying to send it to the console but I am getting null in the console. I am not sure what I am doing right and what I am doing wrong so I was just wondering if I could get some pointers.
This is what I have for the request
$(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);
This is what is in 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"
}
]
};
As I say, the response from the console using console.log is null. I can't see where I am going right or wrong. The request is a snippet I got from a post on here (load json into variable)
Any pointers would be much appreciated
Thanks in advance