?? PHP ?? ????: ??? ?? ???
1. ??? ?? ???
1. XMLHttpRequest ??? ??? ???? ???? ? ?????.
2. ??? ?? ???
??? ??? ??? ?? open() ? send() ???:
xmlhttp.open("GET","test1.txt",true);
xmlhttp.send();
3. GET ?? POST ??
POST? ?? GET? ? ???? ???, ?? ???? ?????.
?, ??? ?? ???? POST ??? ??? ??? ????.
?? ??? ??? ? ????(?? ?? ????) ?? ??????)
??? ??? ???? ??? ??(POST?? ??? ??? ????.)
? ? ?? ??? ??? ??? ??? ??? ?? , POST? GET?? ????? ??? ? ????
???? ? ? ?? ??? ???? ? ??? ?? ?? ?? ???????
4.
- ??? GET ??:
<!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","2_2.php",true); xmlhttp.send(); } </script> </head> <body> <h2>AJAX</h2> <button type="button" onclick="loadXMLDoc()">請(qǐng)求數(shù)據(jù)</button> <div id="myDiv"></div> </body> </html>?? ?? 2_2.php
<?php echo "使用GET方法請(qǐng)求得到的<br/>"; echo date("Y-m-d h:i:s",time()); ?>
- GET ???? ??? ????? URL? ??? ?????:
<!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","2_6.php?name=小明",true); xmlhttp.send(); } </script> </head> <body> <h2>AJAX</h2> <button type="button" onclick="loadXMLDoc()">請(qǐng)求數(shù)據(jù)</button> <div id="myDiv"></div> </body> </html>???? 2_6.php
<?php $name=$_GET['name']; echo "使用GET方法請(qǐng)求得到的<br/>"; echo "你好".$name."同學(xué)"; ?>5. POST ??
- ???? 2_3.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("POST","2_4.php?",true); xmlhttp.send(); } </script> </head> <body> <h2>AJAX</h2> <button type="button" onclick="loadXMLDoc()">請(qǐng)求數(shù)據(jù)</button> <div id="myDiv"></div> </body> </html>2_4.php
<?php echo "使用POST方法請(qǐng)求得到的<br/>"; echo date("Y-m-d h:i:s",time()); ?>
- HTML ??? ?? ???? POST?? ?? ?? setRequestHeader()? ???? HTTP ??? ?????. ?? ?? send() ????? ???? ???? ??????:
<!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("POST","2_8.php",true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send("name=小明&age=22"); } </script> </head> <body> <h2>AJAX</h2> <button type="button" onclick="loadXMLDoc()">請(qǐng)求數(shù)據(jù)</button> <div id="myDiv"></div> </body> </html>2_8.php
<?php $name=$_POST['name']; $age=$_POST['age']; echo "使用POST方法請(qǐng)求得到的<br/>"; echo "大家好,我叫".$name."今年".$age."歲"; ?>
6. ?? ??
(1) url - ??? ?? ??
- open () ???? url ????? ??? ?? ??? ?????:
??? .txt ? .xml? ?? ?? ??? ????? .asp ? .php? ?? ?? ???? ??? ? ????(??? ?? ???? ??? ??? ? ??). ??) .
(2) Asynchronous-true/false
AJAX? Asynchronous JavaScript? XML? ?????.
XMLHttpRequest ??? AJAX? ????? ?? open() ???? async ?? ??? true? ???? ???.
xmlhttp.open("GET","ajax_test.php ",true) ;
??? ?? ??? ? ????? ? ?????. ???? ???? ?? ???? ??? ?? ????. AJAX ???? ?? ?? ??????? ????? ??? ? ?????.
AJAX? ???? JavaScript? ??? ??? ??? ??? ????. ??
?? ??? ?????. ??? ??? ???? ?? ?? ????
??? ???? ??? ?????.
(3) ??? =true
async=true? ???? ?? onreadystatechange ????? ?? ??? ??? ? ??? ??? ?????.
?? ??: 2_9.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","2_10.txt",true); xmlhttp.send(); } </script> </head> <body> <h2>AJAX</h2> <button type="button" onclick="loadXMLDoc()">通過(guò)AJAX改變內(nèi)容</button> <div id="myDiv"></div> </body> </html>
2_10.txt
這是通過(guò)AJAX得到的內(nèi)容?。?!
(4) Async=false
async=false? ???? ?? ?? ??? ?????. open() ???? ? ?? ????? false? ??:
xmlhttp.open("GET","test1.txt",false);
async=false? ???? ?? ???? ??? ?? ?? ??? ???? ?????.
JavaScript? ???? ?? ?? ??? ??? ??? ?????. ??? ???? ??? ??? ??????? ????? ?????.
??: async=false? ???? ?? onreadystatechange ??? ???? ?? send() ? ?? ??? ?????.
?? ??: 2_11 .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.open("GET","2_10.txt",false); xmlhttp.send(); document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } </script> </head> <body> <h2>AJAX</h2> <button type="button" onclick="loadXMLDoc()">請(qǐng)求數(shù)據(jù)</button> <div id="myDiv"></div> </body> </html>
2_10.txt
這是通過(guò)AJAX得到的內(nèi)容!!!