1

所以我正在为我的博客制作一个 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 月。这有点奇怪?

4

2 回答 2

0

根据您的 json 结构,它可能应该是这样的。

for(var month in data){
        var monthEntries = data[month];
                    for(var day in monthEntries)
                    var entry = monthEntries[day];
                    {
                        blog[counter] = "<h4>"+entry[day].title+"</h4><p>"+entry[day].post+"</p>"+"<hr />";
                        title += "<a onClick='willImpletmentlater(" + entry[day] + ")" + "'>" + entry[day].title + "</a><br /><br />"
                        counter = counter + 1;
                    }

                }
于 2012-09-30T01:15:58.363 回答
0

啊,我明白你的意思了。您的 json 响应月份对象不是一个数组,它是一个具有键值对的对象,并且当您执行 var key 时,对象键没有设置的检索顺序...要获得预期的顺序,您应该将 json 响应更改为类似这

   {
     "june": [
        {
            "day": "17",
            "title": "Monday 17 of June 2012",
            "post": "the post taken out to save space."
        },
        {
            "day": "16", 
            "title": "Sunday 16 of June 2012",
            "post": "the post taken out to save space"
        },
        {
            "day": "15",
            "title": "Saturday 15 of June 2012",
            "post": "the post taken out to save space"
        },
        {
            "day":"14",
            "title": "Friday 14 of June 2012",
            "post": "the post taken out to save space"
        },
        {    "day": "13",
            "title": "Thursday 13 of June 2012",
            "post": "the post taken out to save space"
        },
        {    "day": "12",
            "title": "Wednesday 12 of June 2012",
            "post": "the post taken out to save space"
        }
    ]

}

我已经更新了 jsfiddle。

http://jsfiddle.net/ew44p/1/

于 2012-09-30T03:28:22.520 回答