3、Ajax同步请求
同步或者异步
例:xmlhttp.open("GET","ajax_test.html",true);中,其 open() 方法如果async=true则为异步
async=false则为同步。
1.当使用 async=true 时,请规定在响应处于 onreadystatechange 事件中的就绪状态时执行的函数:
xmlhttp.open("GET","/try/ajax/ajax_info.txt",true);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
2.当您使用 async=false 时,请不要编写 onreadystatechange 函数 - 把代码放到 send() 语句后面即可:
由于在同步请求中,当执行到send()语句时,readyState的状态值为4,故不需要回调函数
xmlhttp.open("GET","/try/ajax/ajax_info.txt",false);
xmlhttp.send();
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

浙公网安备 33010602011771号