Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

JavaScript AJAX PHP issue

Ok. I need help with this. For some reason the onreadystatechange is fired multiple times. I really need to get this figured out tonight. It's the last task I have left and I don't know what to do or what's causing it. Please help.

I'm using AJAX (ndhr) to send over JSON 'Y-m-d h:i:s' to PHP to use the strtotime() function to return 'm-d-Y' back through AJAX. The JSON and PHP work great, but when the onreadystatechange is fired it does it multiple times. Almost like the readyState == 4 more times than it does.

var divs_d = ["d_2009", "d_2010", "d_2011"];

function ajax_get_json(cdiv,ocdv,ed){
    var hr = new XMLHttpRequest();
    hr.open("GET", "/json/sample.json", true);
    hr.setRequestHeader("Content-type", "application/json", true);
    hr.onreadystatechange = function () {
        if (hr.readyState == 4 && hr.status == 200) {
            cdiv.innerHTML = "";
            var data = JSON.parse(hr.responseText);
            var cad = data.comm_archive;
            var rndate;
            var nda = new Array();
            var ndac = 0;
            var ec = 0;

            for (ni = 0; ni < cad.length; ni++) {
                if (cad[ni].year == ocdv) {
                    ec = ec + 1;
                    ed.innerHTML = '<h4>' + ocdv + ' (' + ec + ' entries)</h4>';

                    var ndhr = new XMLHttpRequest();
                    var url = "/inc/strtotime.php";
                    var vars = "ndate=" + cad[ni].publish_date;
                    ndhr.open("POST", url, true);
                    ndhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    ndhr.onreadystatechange = function () {
                        if (ndhr.readyState == 4 && ndhr.status == 200) {
                            nda[ndac] = ndhr.responseText;
                            ndac = ndac + 1;
                        }
                    }
                    ndhr.send(vars);
                }
            }

            nda.sort(function (a, b) { return b - a });
            for (ndai = 0; ndai < ndac; ndai++) {
                cdiv.innerHTML += '<h4><a href="/this_is_a_Test/archive.php?cdate=' + nda[ndai] + '">' + nda[ndai] + '</a></h4>';
            }
        }
    }
    hr.send(null);
}

function optionCchange() {
    var ocdv = document.getElementById("optionCdate").value;
    var ed = document.getElementById("ediv");

    for (i = 0; i < divs_d.length; i++) {
        var cdiv = document.getElementById(divs_d[i]);

        if (divs_d[i] == "d_" + ocdv) {
            cdiv.className = "bddiv show";
            ajax_get_json(cdiv,ocdv,ed);
        } else {
            cdiv.className = "bddiv hide";
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your ndhr.onreadystatechange function ndhr represents the last ndhr created in the loop not the calling one, to reference the calling object use this.

 ndhr.onreadystatechange = function () {
     if (this.readyState == 4 && this.status == 200) {
         nda[ndac] = this.responseText;
         ndac = ndac + 1;
     }
 }

The the last for(ndai = 0; ndai < ndac; ndai++) is behaving as you expect because of the asynchronous nature of ajax, by the time that code is executed the ajax requests have not finished yet. You'll have to execute this code in the on ready change state callback. Just use a counter to check if all the ajax requests have finished then execute the code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...