I am using this JavaScript:
$.getJSON("/aTest.json", function (jsonObj) {
$("#testJSONBtn").click(function () {
var val = "";
for (var i = 0; i <= jsonObj.events.length; ++i) {
val += jsonObj.events[i].title + ", " + jsonObj.events[i].date + ", " + jsonObj.events[i].explanation + "<br/>";
}
$("#JSONOutput").append(val);
});
});
To access a json file:
{
"events":
[
{"title":"Okmulgee Public Schools Starts 3rd Quarter" , "date":"1-2-2013" , "explanation":"Okmulgee Public Schools begins its third quarter."},
{"title":"Okmulgee Public Schools-Closed in Observance of Martin Luther King Jr. Holiday" , "date":"1-21-2013" , "explanation":"The Okmulgee Public Schools will be closed in observance of the Martin Luther King Jr. holiday."},
{"title":"Okmulgee Public Schools County Professional Day" , "date":"2-1-2013" , "explanation":"Okmulgee Public Schools County Professional Day is today."}
]
}
After finally getting IIS Express to serve up json files, I have looked the syntax over a thousand times and can't see any errors, however when I try to get this I get:
Uncaught TypeError: Cannot read property 'title' of undefined
It errors on this line of the JavaScript/jQuery function:
val += jsonObj.events[i].title + ", " + jsonObj.events[i].date + ", " + jsonObj.events[i].explanation + "<br/>";
I'm at a loss, I have researched the following webpages and StackOverflow Questions:
Why is my JSON object undefined?
calling Json data returns undefined "
http://api.jquery.com/jQuery.getJSON/
http://www.w3schools.com/json/default.asp
The syntax seems right when matched up with W3Schools' example.