AJAX 被用于创建交互性更强的应用程序。
XMLHttpRequest 对象是 AJAX 的关键。
该对象在 Internet Explorer 5.5 与 2000 年 7 月发布之后就已经可用了,但是在 2005 人们开始讨论 AJAX 和 Web 2.0 之前,这个对象并没有得到充分的认识。
不同的浏览器使用不同的方法来创建 XMLHttpRequest 对象。
Internet Explorer 使用 ActiveXObject。
其他浏览器使用名为 XMLHttpRequest 的 JavaScript 内建对象。
要克服这个问题,可以使用这段简单的代码:
var XMLHttp=null |
if (window.XMLHttpRequest) |
{ |
XMLHttp=new XMLHttpRequest() |
} |
else if (window.ActiveXObject) |
{ |
XMLHttp=new ActiveXObject("Microsoft.XMLHTTP") |
} |
一些程序员喜欢使用最新最快的版本的 XMLHttpRequest 对象。
下面的例子试图加载微软最新版本的 "Msxml2.XMLHTTP",在 Internet Explorer 6 中可用,如果无法加载,则后退到 "Microsoft.XMLHTTP",在 Internet Explorer 5.5 及其后版本中可用。
function GetXmlHttpObject() |
{ |
var xmlHttp=null; |
try |
{ |
// Firefox, Opera 8.0+, Safari |
xmlHttp=new XMLHttpRequest(); |
} |
catch (e) |
{ |
// Internet Explorer |
try |
{ |
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); |
} |
catch (e) |
{ |
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); |
} |
} |
return xmlHttp; |
} |
当用户在上面的输入框中键入字符时,会执行 "showHint()" 函数。该函数由 "onkeyup" 事件触发:
<html> |
<head> |
<script> |
function showHint(str) |
{ |
if (str.length==0) |
{ |
document.getElementById("txtHint").innerHTML=""; |
return; |
} |
if (window.XMLHttpRequest) |
{ |
// IE7+, Firefox, Chrome, Opera, Safari 浏览器执行的代码 |
xmlhttp=new XMLHttpRequest(); |
} |
else |
{ |
//IE6, IE5 浏览器执行的代码 |
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); |
} |
xmlhttp.onreadystatechange=function() |
{ |
if (xmlhttp.readyState==4 && xmlhttp.status==200) |
{ |
document.getElementById("txtHint").innerHTML=xmlhttp.responseText; |
} |
} |
xmlhttp.open("GET","gethint.php?q="+str,true); |
xmlhttp.send(); |
} |
</script> |
</head> |
<body> |
<p><b>在输入框中输入一个姓名:</b></p> |
<form> |
姓名: <input type="text" onkeyup="showHint(this.value)"> |
</form> |
<p>返回值: <span id="txtHint"></span></p> |
</body> |
</html> |
如果输入框是空的(str.length==0),该函数会清空 txtHint 占位符的内容,并退出该函数。
如果输入框不是空的,那么 showHint() 会执行以下步骤:
上面这段通过 JavaScript 调用的服务器页面是名为 "gethint.php" 的 PHP 文件。
"gethint.php" 中的源代码会检查姓名数组,然后向浏览器返回对应的姓名:
<?php |
// 将姓名填充到数组中 |
$a[]="Anna"; |
$a[]="Brittany"; |
$a[]="Cinderella"; |
$a[]="Diana"; |
$a[]="Eva"; |
$a[]="Fiona"; |
$a[]="Gunda"; |
$a[]="Hege"; |
$a[]="Inga"; |
$a[]="Johanna"; |
$a[]="Kitty"; |
$a[]="Linda"; |
$a[]="Nina"; |
$a[]="Ophelia"; |
$a[]="Petunia"; |
$a[]="Amanda"; |
$a[]="Raquel"; |
$a[]="Cindy"; |
$a[]="Doris"; |
$a[]="Eve"; |
$a[]="Evita"; |
$a[]="Sunniva"; |
$a[]="Tove"; |
$a[]="Unni"; |
$a[]="Violet"; |
$a[]="Liza"; |
$a[]="Elizabeth"; |
$a[]="Ellen"; |
$a[]="Wenche"; |
$a[]="Vicky"; |
//从请求URL地址中获取 q 参数 |
$q=$_GET["q"]; |
//查找是否由匹配值, 如果 q>0 |
if (strlen($q) > 0) |
{ |
$hint=""; |
for($i=0; $i<count($a); $i++) |
{ |
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))) |
{ |
if ($hint=="") |
{ |
$hint=$a[$i]; |
} |
else |
{ |
$hint=$hint." , ".$a[$i]; |
} |
} |
} |
} |
// 如果没有匹配值设置输出为 "no suggestion" |
if ($hint == "") |
{ |
$response="no suggestion"; |
} |
else |
{ |
$response=$hint; |
} |
//输出返回值 |
echo $response; |
?> |
如果你的异步请求需要跨域可以查看:PHP Ajax 跨域问题解决方案。