I'm having some trouble accessing my JSON data with an AJAX call.
JSON
{
"Ongoing": [
{"name": "meh"},
{"name": "hem"}
],
"Completed": [
{"name": "kfg"},
{"name": "dkfgd"}
]
}
JS
$.ajax({
type: "GET",
dataType: "json",
url: "script/projects.json",
complete: function(data){
for(var i = 0; i < data.Ongoing.length; i++){
console.log(data.Ongoing[i].name);
}
}
})
When I write to the console, I getting the following error:
TypeError: Cannot read property 'length' of undefined
So for some reason it's kicking back that the array is undefined.
I've passed the JSON into the validator, so I know it's valid JSON. I know that jQuery's $.ajax
parses JSON automatically. So I'm confused, I should be able to access the data like a normal object. Can anyone explain to me what I've done wrong and provide an explanation to how to fix it?
The following image shows what simply writing data
to the console displays.