0

XMLHttpRequestを使用してPHPファイルを呼び出していますが、呼び出しが完了せず、理由がわかりません。これreq.readyStateは4ではありません。PHPファイルは問題なく、想定どおりに動作するため(文字列をエコーするだけ)、理由はわかりません。

私が見えないものを誰かが見ることができますか?

function processAjax(id, option) {
    if (option == "lpath") url = "<?php echo $mosConfig_live_site;?>/administrator/components/com_joomlaquiz/getinfo.php?id=" + id;
    else url = "<?php echo $mosConfig_live_site;?>/administrator/components/com_joomlaquiz/getinfo.php?cat=" + id;

    //create AJAX request
    if (window.XMLHttpRequest) { // Non-IE browsers
        req = new XMLHttpRequest();
        req.onreadystatechange = targetDiv();
        try {
            req.open("GET", url, true);
        } catch (e) {
            alert(e);
        }
        req.send(null);
    } else if (window.ActiveXObject) { // IE
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = targetDiv();
            req.open("GET", url, true);
            req.send();
        }
    }
}
//this function handles the response from the ajax request

function targetDiv() {
    if (req.readyState == 4) { // Complete
        if (req.status == 200) { // OK response
            //all of the code below doesn't happen because its not the option
            if (option == "lpath") {
                var response = req.responseText.split('##');
                var articles = response[0].split(';');
                var quizes = response[1].split(';');

                document.getElementById("article_id").innerHTML = "";
                document.getElementById("quiz_id").innerHTML = "";

                for (var i = 0; i < articles.length; i = i + 2) {
                    if ((i + 1) <= articles.length) {
                        var option = new Option( /* Label */ articles[i + 1], /* Value */ articles[i]);
                        document.getElementById("article_id").options.add(option);
                    }
                }

                for (var i = 0; i < quizes.length; i = i + 2) {
                    if ((i + 1) <= quizes.length) {
                        var option = new Option( /* Label */ quizes[i + 1], /* Value */ quizes[i]);
                        document.getElementById("quiz_id").options.add(option);
                    }
                }

                delete req, articles, quizes;
            } else {
                document.getElementById("catdiv").innerHTML += req.responseText;
                document.getElementById("allchildren").value = req.responseText;
            }
        } else { //failed to get response
            alert("Problem: " + req.statusText);
        }
    }
    document.getElementById("catdiv").innerHTML += "Y U NO COMPLETE?!";
}
4

1 に答える 1

3
req.onreadystatechange = targetDiv();

する必要があります

req.onreadystatechange = targetDiv;

元のコードはtargetDiv()、そのコード行が実行された直後に呼び出されますが、これはおそらくやりたいことではありません。修正されたコードは、Ajax リクエストが受信された後、関数を正しく呼び出します。

于 2011-11-20T22:01:26.640 に答える