ajax.html
<
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gbk" />
< title >Ajax教程 </title>
< script language ="javascript" type ="text/javascript" >
//浏览器支持代码
function ajaxFunction(){
var ajaxRequest; //xmlHttpRequest对象
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// IE系列
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// 古老的浏览器
alert("你的浏览器不支持Ajax!");
return false;
}
}
}
// 创建一个函数负责介绍来自服务器的数据
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.myForm.time.value = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "serverTime.php", true);
ajaxRequest.send(null);
}
</script>
</head>
< body >
< form name ='myForm' >
用户名: < input type ='text' name ='username' onChange ="ajaxFunction();" /> < br />
时间: < input type ='text' name ='time' />
</form>
</body>
</html>
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gbk" />
< title >Ajax教程 </title>
< script language ="javascript" type ="text/javascript" >
//浏览器支持代码
function ajaxFunction(){
var ajaxRequest; //xmlHttpRequest对象
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// IE系列
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// 古老的浏览器
alert("你的浏览器不支持Ajax!");
return false;
}
}
}
// 创建一个函数负责介绍来自服务器的数据
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.myForm.time.value = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "serverTime.php", true);
ajaxRequest.send(null);
}
</script>
</head>
< body >
< form name ='myForm' >
用户名: < input type ='text' name ='username' onChange ="ajaxFunction();" /> < br />
时间: < input type ='text' name ='time' />
</form>
</body>
</html>
serverTime.php:



本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/110842,如需转载请自行联系原作者