½Ó´¥AjaxµÄµÚÒ»¸öʵÑéHello Word
<html>
<head>
<title>new ajax</title>
<mce:style type="text/css"><!--
body{
margin-top:50px;
}
.MyDiv{
font-size:12px;
border:1px solid #DDD;
background-color:#FFD;
margin:2px;
width:200px;
}
--></mce:style><style type="text/css" mce_bogus="1"> body{
margin-top:50px;
}
.MyDiv{
font-size:12px;
border:1px solid #DDD;
background-color:#FFD;
margin:2px;
width:200px;
}
</style>
</head>
<body onload="getResponse()">
<mce:script language="javascript"><!--
var xmlhttp;
//ͨ¹ýһϵķ½Ê½È·±£ÔÚ²»Í¨µÄä¯ÀÀÆ÷Öж¼Äܹ»ÉùÃ÷XMLHttpRequest¶ÔÏó.
try{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
xmlhttp=new ActiveXObject("Microsoft.XHLHTTP");
}catch(e){
try{
xmlhttp=new XMLHttpRequest();
}catch(e){}
}
}
function getResponse(){
//¹¹Ô캯ÊýURL£¬´«ËÍÒ»¸ö±¾µØÊ±¼ä¡£
var dateObj=new Date();
var url="Response.php?time="+dateObj.toLocaleString();
xmlhttp.open("GET",url);
//°ó¶¨showMessage·½·¨µ½onreadystatechangeʼþ¡£
xmlhttp.onreadystatechange=showMessage;
//°ó¶¨Ö®ºóµ÷ÓÃsend·½·¨£¬ÓÐʱµßµ¹Õâ¸ö´ÎÐò»áʹÏìÓ¦µÃ²»µ½´¦Àí¡£
xmlhttp.send();
}
function showMessage(){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
document.getElementById("Result").innerHTML=xmlhttp.responseText;
}
else{
document.getElementById("Result").innerHTML="Error!";
}
}
}
// --></mce:script>
<center>
<div id="Result" class="MyDiv" >Information:</div>
<input type="button" value="Get response" onclick="getResponse()">
</center>
</body>
</html>
·þÎñÆ÷¶ËÏìÓ¦ÎļþRespons.php
<?
//»Ø´«¿Í»§¶Ëʱ¼ä¡¢Hello WordÒÔ¼°·þÎñÆ÷ʱ¼äһͬ·µ»Ø¿Í»§¶Ë
echo "Ajax Test"."<br/>";
echo "myTime:".$_GET["time"]."<br/>";
echo "server:Hello Word!<br/>";
echo "server Time:".$showtime=date("Y-m-d H:i:s");
?>
Ïà¹ØÎĵµ£º
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>xmlhttprequest ajax demo</title>
<script type ="text/javascript" language ="javascript" >
......
¡¡Ò»°ãÇé¿öÏ£¬Ê¹ÓÃAjaxÌá½»µÄ²ÎÊý¶àÊÇЩ¼òµ¥µÄ×Ö·û´®£¬¿ÉÒÔÖ±½ÓʹÓÃGET·½·¨½«ÒªÌá½»µÄ²ÎÊýдµ½open·½·¨µÄurl²ÎÊýÖУ¬´Ëʱsend·½·¨µÄ²ÎÊýΪnull¡£
ÀýÈç £º
¡¡¡¡ var url = "login.jsp?user=XXX&pwd=XXX";
¡¡¡¡xmlHttpRequest.open("GET",url,true);
¡¡¡¡xmlHttpRequset.send(null);
¡¡¡¡´ËÍ⣠......
//Óû§ÃûУÑéµÄ·½·¨
//Õâ¸ö·½·¨½«Ê¹ÓÃXMLHTTPRequest¶ÔÏóÀ´½øÐÐAJAXµÄÒì²½Êý¾Ý½»»¥
var xmlhttp;
function verify() {
//0¡£Ê¹ÓÃdomµÄ·½Ê½»ñÈ¡Îı¾¿òÖеÄÖµ
//document.getElementById("userName")ÊÇdomÖлñÈ¡ÔªËØ½ÚµãµÄÒ»ÖÖ·½·¨£¬Ò»¸öÔªËØ½Úµã¶ÔÓ¦HTMLÒ³ÃæÖеÄÒ»¸ö± ......
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
......