【发布时间】:2025-12-27 07:00:07
【问题描述】:
我有一个 ajax 函数,我只是调用一个 php 页面,该页面有一个要运行的 mysql 事务。在检查 mysql 日志时,我已验证事务运行成功但 xmlhttp 对象跳转到 else 语句 readyState 和状态。
我的js代码:
function promoteOptionsAllot(stid,cid,nsid,elid,flag){
anchor = document.getElementById('promoteAnchor'+elid);
imgContainer = document.getElementById('promoteStatus'+elid);
if(flag == 1){
opt1 = encodeURIComponent(document.getElementById('promote_option1').value);
opt2 = encodeURIComponent(document.getElementById('promote_option2').value);
opt3 = encodeURIComponent(document.getElementById('promote_option3').value);
opt4 = encodeURIComponent(document.getElementById('promote_option4').value);
params = "stid="+encodeURIComponent(stid)+"&course="+encodeURIComponent(cid)+"&sem="+encodeURIComponent(nsid)+"&element="+encodeURIComponent(elid)+"&popt1="+opt1+"&popt2="+opt2+"&popt3="+opt3+" &popt4="+opt4+"&flag="+encodeURIComponent(flag);
}
else if(flag == 2){
params = "stid="+encodeURIComponent(stid)+"&course="+encodeURIComponent(cid)+"&sem="+encodeURIComponent(nsid)+"&element="+encodeURIComponent(elid)+"&flag="+encodeURIComponent(flag);
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.body.removeChild(document.getElementById('prBox'));
document.body.removeChild(document.getElementById('prBackground'));
result = xmlhttp.responseText;
if(result == "done"){
anchor.innerHTML = "<span style='color:#198D19;'><b>Promoted</b></span>";
imgContainer.src = "tick.png";
}else {
alert("There was a problem serving the request. Please try again.");
imgContainer.src = "cross.jpg";
}
}
else{
imgContainer.src = "alert.gif";
}
}
xmlhttp.open("POST","promoptallot.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(params);
}
它是链接的 onclick 事件,并且在单击链接时虽然交易成功,但不知何故,imagecontainer 会显示 alert.gif,这意味着它永远不会在 readystate == 4 和 status == 200 语句中运行代码。
请不要建议使用 jquery。我知道它是一个稳定的框架和其他东西,但我只需要使用它来回答。
【问题讨论】:
-
您是否在交易后在您的 PHP 脚本中回显“完成”?
if (result == "done")我会尝试console.log(result)来查看您的 PHP 脚本正在输出什么。它可能输出的不仅仅是确切的字符串“done” -
@crush 是的,我在交易完成后回显完成
-
好的,我重读了你的问题,我看到你说“它永远不会在 readystate == 4 和 status == 200 语句中运行代码。”
-
@crush 那么你认为现在发生了什么?
-
您是否使用同一个 xmlhttp 对象处理多个 AJAX 查询?你在哪里声明 xmlhttp?另外,我认为你应该在 if 块之前输出
xmlhttp.status和xmlhttp.readyState,这样你就可以看到它们的值是什么。
标签: php mysql ajax readystate