AJAX - ?? ??
????? ??? ???? XMLHttpRequest ??? responseText ?? responseXML ??? ?????.
ResponseText ? ResponseXML
?? ???? ??? XML, ?? ???, (X)HTML ?? JSON(JavaScript Object Notation)? ?? ??? ???? ??? ? ????. ??? ???? ??? ?? responseText ?? responseXML? ???? ? ?? ???? ??? ? ????.
responseText ???? XML ??? ?? ??? ?????. ?? ??? ???? ??? ??? ??? ?????. ?? ???, (X)HTML ? JSON? ?? responseText? ?????. responseText? ???? ???? ??? ? ?? ?? ??? AJAX ??? ???? ?? ?????. ?? ?? XML ???? JSON ????? ???? ?? ??? ??? ?? ?? ??? ??? ???? ??? ??? ?? ??? ? ????. ?? ???? ?? ?? ???? ???? ??? ?????. ?? ?? ??? ?? ?? ??? ????? ?? ???.
responseXML? ??? ?? ?????. ??? JSON ??? ????? XML?? ??? ??? ?????. ?? ?? ?? ? ?? XML ???? ?? ??? ???? ????.
var response = ajax.request.responseXML.documentElement;
???? ?? ??? ?????? ???? ?? ?? ?????.
var header = response.getElementsByTagName('header')[0] .firstChild .data;
var ?? = response.getElementsByTagName('description')[0].firstChild.data;
var sourceUrl = response.getElementsByTagName('sourceUrl')[0]. data;
????? ?? div ??? ?? ??? ?????.
$("body").html("<b>" + header + "</b>< br/> ;"
??????????????????????????????????????????????????????????;
responseText ??? ??? ???? ?????.
<!DOCTYPE html> <html> <head> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","ajax_info.txt",true); xmlhttp.send(); } </script> </head> <body> <div id="myDiv"><h2>使用 AJAX 修改該文本內(nèi)容</h2></div> <button type="button" onclick="loadXMLDoc()">修改內(nèi)容</button> </body> </html>??? ??? XML?? XML ??? ?? ???? ?? ?? responseXML ?? ? ???? cd_catalog.xml ??? ?? ??? ?? ?????.
<!DOCTYPE html> <html> <head> <script> function loadXMLDoc() { var xmlhttp; var txt,x,i; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { xmlDoc=xmlhttp.responseXML; txt=""; x=xmlDoc.getElementsByTagName("ARTIST"); for (i=0;i<x.length;i++) { txt=txt + x[i].childNodes[0].nodeValue + "<br>"; } document.getElementById("myDiv").innerHTML=txt; } } xmlhttp.open("GET","cd_catalog.xml",true); xmlhttp.send(); } </script> </head> <body> <h2>My CD Collection:</h2> <div id="myDiv"></div> <button type="button" onclick="loadXMLDoc()">Get my CD collection</button> </body> </html>
?:
<!DOCTYPE HTML> <html> <head> <meta charset=utf-8" /> <title>Using responseText with innerHTML</title> <script type="text/javascript"> var xmlHttp; function createXMLHttpRequest(){ if(window.ActiveXObject){ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }else if(window.XMLHttpRequest){ xmlHttp = new XMLHttpRequest(); } } function startRequest(){ createXMLHttpRequest(); xmlHttp.onreadystatechange = handleStateChange; xmlHttp.open("GET","innerHTML.xml",true); xmlHttp.send(null); } function handleStateChange(){ if(xmlHttp.readyState == 4){ if(xmlHttp.status == 200){ document.getElementById("results").innerHTML=xmlHttp.responseText; } } } </script> </head> <body> <form> <input type="button" value="Search for Today's Activities" onclick="startRequest();"/> </form> <div id="results"></div> </body> </html>
innerHTML.xml ??
<?xml version="1.0" encoding="utf-8"?> <table border="1"> <tbody> <tr><th>Activity Name</th><th>Location</th><th>Time</th></tr> <tr><td>Waterskiing</td><td>Dock #1</td><td>9:00 AM</td></tr> <tr><td>Volleyball</td><td>East Court</td><td>2:00 PM</td></tr> <tr><td>Hiking</td><td>Trail 3</td><td>3:30 PM</td></tr> </tbody> </table>