php - XMLHttpRequest 未完成

标签 php javascript ajax xmlhttprequest readystate

我正在使用 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?!";
}

最佳答案

req.onreadystatechange = targetDiv();

应该是

req.onreadystatechange = targetDiv;

原始代码在该行代码运行后立即调用targetDiv(),这可能不是您想要做的。修复后的代码在收到Ajax请求后正确调用该函数。

关于php - XMLHttpRequest 未完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8204990/

相关文章:

javascript - 对 Google map 进行皮肤处理,使其看起来像原生 Google map iPhone 应用程序

javascript - ajax 之后重新初始化 jCarousel

javascript - AJAX 教程不工作

javascript - 如何导入 popper.js?

php - 在 PHP 中的数组末尾添加一个数组

php - 我怎样才能让我的网络接收mysql数据

php - Symfony - twig - 自动加载器预期类

javascript - 从 Webix 中结果对象的属性加载数据

javascript - Ajax调用不将数据存储在数组中

php - 用php打印整个mysql表