在頁面加載完畢時同時執行2段ajax代碼 結果只能執行一段

原來的代碼:

 

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是異步,就是不等上面執行完,

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章