AJAX ?? ?? ??
ajax ?? ?? ???:
XMLHttpRequest ??? responseText ?? responseXML ??? ???? ???? ?? ???? ?????.
? ?? ?? ?? ??? ??? ????.
Attribute | Description |
responseText | ??? ???? ?? ???? ?????. |
responseXML | ?? ???? XML ???? ?????. |
一.responseText ??:
??? ?? ???? XML? ?? ?? responseText ??? ???? ?????. ? ??? ?? ?? ??? ?????. ???? ??? ????.
document.getElementById("show").innerHTML=xmlhttp.responseText;
?? ?? ?:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.miracleart.cn/" /> <title>php中文網</title> <script> function loadXMLDoc(){ var xmlhttp; if (window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); } else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function(){ if(xmlhttp.readyState==4 && xmlhttp.status==200){ document.getElementById("show").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","demo/ajax/txt/demo.txt",true); xmlhttp.send(); } window.onload=function(){ var obt=document.getElementById("bt"); obt.onclick=function(){ loadXMLDoc(); } } </script> </head> <body> <div id="show"><h2>原來的內容</h2></div> <button type="button" id="bt">查看效果</button> </body> </html>
??? ???? ??? ??? ???? ???? responseText ??? ?? div? ? ? ????.
2. responseXML ??:
??? ??? XML?? XML ??? ?? ????? ?? ?? responseXML ??? ???? ???.
var xmlDoc = xmlhttp.responseXML;
?? ? responseXML ?? ? XML ??? ?? ?? ?? ????? ??? ????.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="author" content="http://www.miracleart.cn/" /> <title>php中文網</title> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var xmlDoc = xmlhttp.responseXML; var str = ""; var targets = xmlDoc.getElementsByTagName("target"); for (index = 0; index < targets.length; index++) { str = str + targets[index].childNodes[0].nodeValue + "<br>"; } document.getElementById("show").innerHTML = str; } } xmlhttp.open("GET", "demo/ajax/xml/XML.xml", true); xmlhttp.send(); } window.onload = function () { var obt = document.getElementById("bt"); obt.onclick = function () { loadXMLDoc(); } } </script> </head> <body> <div> <div id="show"></div> <input id="bt" type="button" value="查看效果"/> </div> </body> </html>