Recently, I was working on creating a JavaScript AJAX loader for my blog. Unfortunately, it's not functioning as expected. Initially, everything was working fine until I decided to incorporate months into the parser and JSON file. From that point onward, the loader simply stopped loading.
Here is the snippet of JavaScript code:
firstTime = 1;
function ajax_get_json_for_main_page(a){
var counter = 0;
var results = document.getElementById("blogShow");
var hr = new XMLHttpRequest();
hr.open("GET", "articles/main.json", true);
hr.setRequestHeader("Content-type", "application/json", true);
hr.onreadystatechange = function() {
var blog = new Array()
var title = "";
if(hr.readyState == 4 && hr.status == 200) {
var data = JSON.parse(hr.responseText);
for(var obj in data){
for(var entry in data[obj])
{
blog[counter] = "<h4>"+entry[obj].title+"</h4><p>"+entry[obj].post+"</p>"+"<hr />";
title += "<a onClick='willImpletmentlater(" + entry[obj] + ")" + "'>" + entry[obj].title + "</a><br /><br />"
counter = counter + 1;
}
}
document.getElementById("big3").innerHTML = title;
if(firstTime == 1)
{
results.innerHTML = blog[0]
firstTime = 1000;
}
if(a == 1)
{
interval = setInterval(function(){blogShowAnimateInner(1, blog);}, 1);
}
if(a == 2)
{
interval = setInterval(function(){blogShowAnimateInner(2, blog);}, 1);
}
if(a == 3)
{
interval = setInterval(function(){blogShowAnimateInner(3, blog);}, 1);
}
if(a == 4)
{
interval = setInterval(function(){blogShowAnimateInner(4, blog);}, 1);
}
}
}
hr.send(null);
}
Additionally, here is the JSON content being used:
{
"june": {
"17": {
"title": "Monday 17 of June 2012",
"post": "the post taken out to save space."
},
"16": {
"title": "Sunday 16 of June 2012",
"post": "the post taken out to save space"
},
"15": {
"title": "Saturday 15 of June 2012",
"post": "the post taken out to save space"
},
"14": {
"title": "Friday 14 of June 2012",
"post": "the post taken out to save space"
},
"13": {
"title": "Thursday 13 of June 2012",
"post": "the post taken out to save space"
},
"12": {
"title": "Wednesday 12 of June 2012",
"post": "the post taken out to save space"
},
"11": {
"title": "Tuesday 11 of June 2012",
"post": "the post taken out to save space"
},
"10": {
"title": "Monday 10 of June 2012",
"post": " the post taken out to save space."
}
}
}
Despite trying Jsfiddle, I realized they do not support AJAX loading. Thus, I couldn't find any hints there.
Edit: Now, the code seems to be working thanks to implementing changes suggested earlier.
A key part of the parse logic:
If(hr.readyState == 4 && hr.status == 200) { var data = JSON.parse(hr.responseText);
var blog = [];
var title = "";
var counter = 0;
for (var month in data) {
var monthEntries = data[month];
for (var i = 0; i < monthEntries.length; i++) {
var entry = monthEntries[i];
blog[counter] = "<h4>" + entry.title + "</h4><p>" + entry.post + "</p>" + "<hr />";
title += "<a onClick='willImpletmentlater(" + entry + ")" + "'>" + entry.title + "</a><br /><br />"
counter = counter + 1;
}
}
console.log(blog);
console.log(title);
document.getElementById("big3").innerHTML = title;
if(a == 1)
{
interval = setInterval(function(){blogShowAnimateInner(1, blog);}, 1);
}
if(a == 2)
{
interval = setInterval(function(){blogShowAnimateInner(2, blog);}, 1);
}
if(a == 3)
{
interval = setInterval(function(){blogShowAnimateInner(3, blog);}, 1);
}
if(a == 4)
{
interval = setInterval(function(){blogShowAnimateInner(4, blog);}, 1);
}
}
}
Edit:
Everything appears to be functioning correctly
Edit:
Oddly enough, adding 'July' causes issues; specifically, the loader stops working. Quite strange, isn't it?