gpt4 book ai didi

php - XMLHttpRequest 未完成

转载 作者:行者123 更新时间:2023-11-28 21:13:09 28 4
gpt4 key购买 nike

我正在使用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com