您的位置:首页 > 编程语言

在页面加载完毕时同时执行2段ajax代码 结果只能执行一段

2011-05-25 16:13 537 查看
原来的代码:

 

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

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息