所以我正在为我的博客制作一个 JavaScript AJAX 加载器,但它不起作用。它以前可以工作,但是当我尝试在解析器和 json 文件中添加几个月时,它刚刚停止并且不会加载。
这是JavaScript:
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);
}
还有 JSON:
{
"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."
}
}
}
我试图继续 Jsfiddle 但我不认为他们加载 ajax?无论哪种方式,他们都没有给我任何提示。
编辑:我现在正在工作这里是建议的主要解析位。
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);
}
}
}
编辑:
现在一切都好
编辑:
如果我添加 7 月不起作用,它只适用于 6 月。这有点奇怪?