PHP development...LOGIN

PHP development basic tutorial server response

1. How to get the server response

If you want to get the server's response, you can use the responseText or responseXML attribute of the XMLHttpRequest object.


66.png

##2. responseText attribute

If it comes from the server The response is not XML, use the responseText attribute.

responseText property returns the response as a string, so you can use it like this:

Use 4_1.php to read information from 4_2.txt

4_1. php code

<!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 code

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

3. responseXML attribute

If the response from the server is XML and needs to be parsed as an XML object, please use the responseXML attribute:

Request the 4_4.xml file and parse the response (

In layman's terms, it is in 4_3.php page does not refresh to read the response content in 4_4.xml ):

4_3.php code

<!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 code

<!--  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>
Next Section
<!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>
submitReset Code
ChapterCourseware