0

我有一个 ajax 函数,我只是调用一个要运行 mysql 事务的 php 页面。在检查 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。我知道它是一个稳定的框架和其他东西,但我只需要一个答案。

4

1 回答 1

0

检查控制台,我发现问题在线document.body.removeChild(document.getElementById('prBox')); document.body.removeChild(document.getElementById('prBackground'));

这些行必须基于案例执行,我没有将其放在 if 语句中。

现在,在if (flag ==1){ document.body.removeChild(document.getElementById('prBox')); document.body.removeChild(document.getElementById('prBackground')); }按预期工作之后。

谢谢。

于 2013-01-11T15:17:34.213 回答