原來的代碼:
function getcourse()
{
sendmsg="coursecategid="+document.getElementById("coursecate").value;
// window.alert(sendmsg);
S_xmlhttprequest();
$url=_webRoot_+"/main/getcourse/";
//$url="http://localhost/rr/main/getcourse/";
//alert($url);
xmlHttp.open("POST",$url,true);
xmlHttp.onreadystatechange=courselist;
xmlHttp.setrequestheader("content-length",sendmsg.length);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(sendmsg);
}
function getemployees()
{
senddeptmsg="deptid="+document.getElementById("deptlist").value;
S_xmlhttprequest();
$url=_webRoot_+"/main/getemployees/";
xmlHttp.open("POST",$url,true);
xmlHttp.onreadystatechange=employeeslist;
xmlHttp.setrequestheader("content-length",senddeptmsg.length);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(senddeptmsg);
}
window.onload=function()
{
getemployees();
getcourse();
}
原因分析:應該是2次請求後臺有衝突,所以改爲順序發送請求,以前是並行發送
修改
function getemployees()
{
senddeptmsg="deptid="+document.getElementById("deptlist").value;
S_xmlhttprequest();
$url=_webRoot_+"/main/getemployees/";
xmlHttp.open("POST",$url,false);
xmlHttp.onreadystatechange=employeeslist;
xmlHttp.setrequestheader("content-length",senddeptmsg.length);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(senddeptmsg);
}
xmlHttp.open("POST",$url,false); 這個是同步,就是第一個方法執行完,才繼續走下面的程序
xmlHttp.open("POST",$url,true); true是異步,就是不等上面執行完,