JavaScript AJAX PHP问题 [英] JavaScript AJAX PHP issue

查看:52
本文介绍了JavaScript AJAX PHP问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的.我需要这方面的帮助.由于某种原因,onreadystatechange被触发了多次.我真的需要今晚弄清楚这个问题.这是我剩下的最后一项任务,我不知道该怎么办或导致了什么.请帮忙.

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.

我正在使用AJAX(ndhr)将JSON'Y-m-d h:i:s'发送到PHP,以使用strtotime()函数通过AJAX返回'm-d-Y'.JSON和PHP很好用,但是当onreadystatechange被触发时,它会多次执行.几乎比readyState ==快4倍.

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";
        }
    }
}

推荐答案

在您的 ndhr.onreadystatechange 函数中, ndhr 表示最后创建的 ndhr 在不是调用方的循环中,要引用调用对象,请使用 this .

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;
     }
 }

由于ajax的异步特性,最后一个for(ndai = 0; ndai< ndac; ndai ++)的行为与您预期的一样,在执行代码时,ajax请求已经具有还没完成.您必须在准备就绪的更改状态回调中执行此代码.只需使用计数器检查所有ajax请求是否已完成,然后执行代码即可.

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.

这篇关于JavaScript AJAX PHP问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆