PHP開發基礎教學之伺服器回應

一、如何取得伺服器回應

想要取得伺服器的回應,可以使用XMLHttpRequest 物件的 responseText 或 responseXML 屬性。


66.png

如果來自伺服器的回應並非XML,請使用responseText 屬性。

responseText 屬性傳回字串形式的回應,因此您可以這樣使用:利用4_1.php  從4_2.txt中讀取訊息

4_1. php 程式碼

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<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","4_2.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">通过AJAX改变内容</button>
<div id="myDiv">AJAX</div>
</body>
</html>

4_2.txt程式碼

* AJAX 是一种用于创建快速动态网页的技术。

三、responseXML屬性

############## #如果來自伺服器的回應是XML,而且需要作為XML 物件進行解析,請使用responseXML 屬性:######請求4_4.xml 文件,並解析回應(###通俗的說就是在#### ##4_3.php頁面不刷新讀取4_4.xml裡面的回應內容######):###############4_3.php程式碼###### ###
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<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("title");
    for(i=0;i<x.length;i++){
    		txt=txt+x[i].childNodes[0].nodeValue+"<br/>";
    	}
    document.getElementById("myDiv").innerHTML=txt
    }
  }
xmlhttp.open("GET","4_4.xml",true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">通过AJAX改变内容</button>
<div id="myDiv">AJAX</div>
</body>
</html>
###### 4_4.xml程式碼######
<!--  Copyright  php.cn -->
<bookstore>
<book category="children">
<title>Harry Potter</title
><author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="cooking">
<title>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="web" cover="paperback">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book><book category="web">
<title>XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
</bookstore>
繼續學習
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <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("title"); for(i=0;i<x.length;i++){ txt=txt+x[i].childNodes[0].nodeValue+"<br/>"; } document.getElementById("myDiv").innerHTML=txt } } xmlhttp.open("GET","4_4.xml",true); xmlhttp.send(); } </script> </head> <body> <h2>AJAX</h2> <button type="button" onclick="loadXMLDoc()">通过AJAX改变内容</button> <div id="myDiv">AJAX</div> </body> </html>
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!