19-3 ?本 AJAX ?????

p`Npϥΰ AJAX ӹFDPBǿ骺\CAJAX ϥΤ覡ADn]tTӰ򥻨BJG
  1. ݡ]Τݡ^oeơGtdbDƥA]w AJAX AùAoe request HҰʦAݵ{XC
  2. ݡ]Aݡ^{XGq`O@ ASP AtdbA楲nBJAҦpˬdbKXAάOƮwidߵC
  3. ݡ]Τݡ^ơGtdA浲GAñNGHDPB覡ܦbDWC
L AJAX Τ覡pAWzTӨBJO|ܤƪ򥻭nCHUڭ̱NϥΤ@²檺dҨӻoTӰ򥻨BJG

Example]ajax/asyncViaAjax01.htm^G

bWzdҤAAIuϥ AJAX ܦAɶvAN|ݨAɶwgܦbDWApUG

ӳoӹL{èSgѺӹFCdҪlXpUG

lɡ]ajax/asyncViaAjax01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>ϥ AJAX ܦAɶ</h2>
<hr>

<script>
// ΤݪơAtdHDPB覡ӱA^ǪƨܦbW
function displayTime() {
	if (ajax.readyState==4)
		if (ajax.status==200)
			document.getElementById('showResult').innerHTML = ajax.responseText;
		else
			alert ("AoͿ~ALk^ǸơI");
}

// ΤݪoeơAtd]w AJAX IJoA
function getServerTime(url) {
	ajax = new ActiveXObject("Msxml2.XMLHTTP");
	ajax.onReadyStateChange=displayTime;	// ]wAƪ^
	ajax.open("GET", url, true);		// ]w ajax 󪺰Ѽ
	ajax.send("");				//  ajax
}
</script>

<input type="button" value="ϥ AJAX ܦAɶ" onClick="getServerTime('showTime.asp')">
<div id="showResult"></div>

</body>
</html>

ezTӰ򥻨BJAڭ̥iHCXƩκApUG

  1. ݪoeơGgetServerTime()C
  2. ݪ{XGshowTime.aspC
  3. ݪơGdisplayTime()C
HUNoXӨƩκy{C
  1. ݪoeƬO getServerTime()ADntdϥΪIsA AJAX ó]wAMAoX requestApUG
    1. Aڭ̨ϥ ajax = new ActiveXObject("Msxml2.XMLHTTP"); Ӳͤ@ AJAX AQΦAڭ̥iHFDPBǿ骺\C

      Hint
      SO`NOA AJAX 󪺤覡AbPs]p IE FireFox^PkAƦܦPˬO IE sAP]P覡Ӳ AJAX AdҩҨϥΪ覡AAΩ IE 6.0 ᪺CFBzoǬۮeʪDAڭ̫ijĥ JavaScript FrameworkAҦp Prototype.jsAoDzӸ`AbU@p`СC

    2. AJAX 󤧫Aڭ̧Yi惡]wUةʽCAڭ̨ϥ ajax.onReadyStateChange=displayTime; ӳ]wA^ơAbdҤAƬO displayTime()C
    3. 䦸Aڭ̨ϥΤUC覡ӳ]w AJAX 󪺨䥦ʽG ajax.open("GET", url, true); 䤤 "GET" Nƶǻ覡Aurl NA{XҦb]bҬ showTime.asp^A true hNϥΫDPBǿC]YO falseANϥΦPBǿC^
    4. ̫Aڭ̨ϥ ajax.send(""); ӰeX AJAX ROAyܻAɷ|ҰʦA{X]bҬ showTime.asp^AMAҰʥΤݪơ]bҬ displayTime()^ANGHDPB覡ܦbثeC
  2. ݪ{X showTime.aspAHҦӨA\۷²AuOLX{bɶAepUG

    lɡ]ajax/showTime.asp^G]ǦϰUYi^
    <%@language=jscript%>
    <%//tdŪثeɶ AJAX ݵ{%>
    <%
    today=new Date();
    time=today.toString();
    Response.write("<font color=red>"+time+"</font>");
    %>
    

  3. ݪƬO displayTime()Ay{yL@ǡG
    1. ˬdA{XO_槹G if (ajax.readyState==4) readyState N ajax ثeAACpUG
      readyState
      0|ҩl
      1wgإ߳s
      2ݤwgnD
      3ݵ{XBz
      4Bz
    2. ~AƤ]ˬdA^ƬO_L~G if (ajax.status==200) ]NOAu showTime.asp ^ǪAXO 200 ɡA~N showTime.asp L~C
    3. @L~Aڭ̤~N AJAX ^ǪrT]s ajax.responseText^w id showResult ϶eG document.getElementById('showResult').innerHTML = ajax.responseText; ɧڭ̴NiHbDݨ컷ݪɶC

      Hint
      A{X^ǪlTA|ʤʦa responseText ʽ褤CpGA^ǪTO XML 榡Aڭ̥iHѥt@өʽ responseXML Ū XML 󤺪UӪC


JScript {]pPΡGΩAݪ ASP