AJAX的true异步或者false同步
ajax的true异步或者false同步:
在ajax简单介绍一章节介绍过,AJAX指的是异步 javascript 和 XML(Asynchronous JavaScript and XML)。
这对于web开发来说是一个非常大的进步,可以有效的提高网站的人性化程度,在此之前,如果有比较费时的请求操作,必然会引起程序挂起和停止的现象,那么使用ajax的异步操作就无需等待服务器的响应,而是进行如下操作:
(1).在等待服务器响应时执行其他脚本。
(2).当响应就绪后对响应进行处理。
一.关于open()方法:
下面再对open()方法做一下简单介绍,语法结构如下:
xmlhttp.open(method,url,async);
关于此方法更多内容可以参阅ajax的post或者get服务器请求一章节。
可以看到open()方法具有三个参数,最后一个参数是一个布尔值,就是用来规定是否采用异步方式。
当async=true的时候,也就是规定采用异步操作,也就是说ajax操作并不会阻塞代码的执行,等执行处理完毕通过onreadystatechange事件对响应进行处理,代码实例如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.php.cn/" />
<title>php中文网</title>
<script>
function loadXMLDoc(){
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("show").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/asset/demo.ajax.php?dm=txt&act=getfruits",true);
xmlhttp.send();
}
window.onload=function(){
var obt=document.getElementById("bt");
obt.onclick=function(){
loadXMLDoc();
}
}
</script>
</head>
<body>
<div id="show"><h2>原来的内容</h2></div>
<button type="button" id="bt">查看效果</button>
</body>
</html>上面的代码就是进行的一个异步操作,通过onreadystatechange事件来对响应进行处理。
当async=false的时候,采用的是同步处理,那么就没有必使用onreadystatechange事件,把相应的操作代码放在send()方法之后即可,代码如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.php.cn/" />
<title>php中文网</title>
<script>
function loadXMLDoc() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "/asset/demo.ajax.php?dm=txt&act=getfruits", false);
xmlhttp.send();
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("show").innerHTML = xmlhttp.responseText;
}
}
window.onload = function () {
var obt = document.getElementById("bt");
obt.onclick = function () {
loadXMLDoc();
}
}
</script>
</head>
<body>
<div id="show"><h2>原来的内容</h2></div>
<button type="button" id="bt">查看效果</button>
</body>
</html>上面的代码并没有采用异步操作,如果ajax操作比较耗时的话,可能会导致代码堵塞的现象,所以不推荐使用。
很多初学者对两者的差别可能还不够明了,并且上面代码也没有很好的演示这一点,下面就通过两段代码演示一下它们的差别:
代码实例一:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>php中文网</title>
<script>
function loadXMLDoc() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("show").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "demo/ajax/net/Async.aspx", true);
xmlhttp.send();
}
window.onload = function () {
loadXMLDoc();
var odiv = document.getElementById("content");
odiv.innerHTML = "由于是异步操作,所以不会阻塞当前内容的显示。";
}
</script>
</head>
<body>
<div id="show"><img src="wait.gif"></div>
<div id="content"></div>
</body>
</html>上面的代码是异步操作,所以当ajax请求的代码在后台处理的时候,并不影响其他代码的执行,所以等待响应的时候,依然能够在下面的div中写入指定的内容,这就是ajax的一个重大有点。
代码实例二:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.php.cn/" />
<title>php中文网</title>
<script>
function loadXMLDoc() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "demo/ajax/net/Async.aspx", false);
xmlhttp.send();
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("show").innerHTML = xmlhttp.responseText;
}
}
window.onload = function () {
loadXMLDoc();
var odiv = document.getElementById("content");
odiv.innerHTML = "由于是同步操作,所以会阻塞当前内容的显示。";
}
</script>
</head>
<body>
<div id="show"><img src="wait.gif"></div>
<div id="content"></div>
</body>
</html>上面的代码在进行ajax后台操作的时候,并不能执行下面的代码,只能等待处理完毕,再去执行后面的代码。
上例中所有调用的文件都可在本地创建更改使用。

面对疾风吧
最学不会的就是AJAX
8年前 添加回复 0