PHP開發(fā)基礎(chǔ)教程之向服務(wù)器發(fā)送請求
一、向服務(wù)器發(fā)送請求
1、XMLHttpRequest 對象用于和服務(wù)器交換數(shù)據(jù)。
2、向服務(wù)器發(fā)送請求
?如需將請求發(fā)送到服務(wù)器,我們使用 XMLHttpRequest 對象的 open() 和 send() 方法:
????xmlhttp.open("GET","test1.txt",true);
? ? xmlhttp.send();
3、使用GET 還是 POST
與 POST 相比,GET 更簡單也更快,并且在大部分情況下都能用。
然而,在以下情況中,請使用 POST 請求:
無法使用緩存文件(更新服務(wù)器上的文件或數(shù)據(jù)庫)
向服務(wù)器發(fā)送大量數(shù)據(jù)(POST 沒有數(shù)據(jù)量限制)
發(fā)送包含未知字符的用戶輸入時,POST 比 GET 更穩(wěn)定也更可靠
接下來,通過實際例子來帶大家理解這兩種請求
4、GET請求
一個簡單的GET請求:
源碼2_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","2_2.php",true); xmlhttp.send(); } </script> </head> <body> <h2>AJAX</h2> <button type="button" onclick="loadXMLDoc()">請求數(shù)據(jù)</button> <div id="myDiv"></div> </body> </html>
源碼2_2.php
<?php echo "使用GET方法請求得到的<br/>"; echo date("Y-m-d h:i:s",time()); ?>
如果您希望通過 GET 方法發(fā)送信息,請向 URL 添加信息:
源碼2_5.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_6.php?name=小明",true); xmlhttp.send(); } </script> </head> <body> <h2>AJAX</h2> <button type="button" onclick="loadXMLDoc()">請求數(shù)據(jù)</button> <div id="myDiv"></div> </body> </html>
源碼2_6.php
<?php $name=$_GET['name']; echo "使用GET方法請求得到的<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()">請求數(shù)據(jù)</button> <div id="myDiv"></div> </body> </html>
?2_4.php
<?php echo "使用POST方法請求得到的<br/>"; echo date("Y-m-d h:i:s",time()); ?>
如果需要像 HTML 表單那樣 POST 數(shù)據(jù),請使用 setRequestHeader() 來添加 HTTP 頭。然后在 send() 方法中規(guī)定您希望發(fā)送的數(shù)據(jù):
源碼2_7.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_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()">請求數(shù)據(jù)</button> <div id="myDiv"></div> </body> </html>
2_8.php
<?php $name=$_POST['name']; $age=$_POST['age']; echo "使用POST方法請求得到的<br/>"; echo "大家好,我叫".$name."今年".$age."歲"; ?>
6、名詞解釋
(1)url-服務(wù)器上的文件
?open() 方法的 url 參數(shù)是服務(wù)器上文件的地址:
xmlhttp.open("GET","ajax_test.php",true);
該文件可以是任何類型的文件,比如 .txt 和 .xml,或者服務(wù)器腳本文件,比如 .asp 和 .php (在傳回響應(yīng)之前,能夠在服務(wù)器上執(zhí)行任務(wù))。
(2)異步-true/false
AJAX 指的是異步 JavaScript 和 XML(Asynchronous JavaScript and XML)。
XMLHttpRequest 對象如果要用于 AJAX 的話,其 open() 方法的 async 參數(shù)必須設(shè)置為 true:
xmlhttp.open("GET","ajax_test.php",true);
對于 web 開發(fā)人員來說,發(fā)送異步請求是一個巨大的進步。很多在服務(wù)器執(zhí)行的任務(wù)都相當費時。AJAX 出現(xiàn)之前,這可能會引起應(yīng)用程序掛起或停止。
通過 AJAX,JavaScript 無需等待服務(wù)器的響應(yīng),而是:
在等待服務(wù)器響應(yīng)時執(zhí)行其他腳本
當響應(yīng)就緒后對響應(yīng)進行處理
(3)Async=true
當使用 async=true 時,請規(guī)定在響應(yīng)處于 onreadystatechange 事件中的就緒狀態(tài)時執(zhí)行的函數(shù):
源碼: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()">通過AJAX改變內(nèi)容</button> <div id="myDiv"></div> </body> </html>
??2_10.txt
這是通過AJAX得到的內(nèi)容!??!
?
(4)Async=false
如需使用 async=false,請將 open() 方法中的第三個參數(shù)改為 false:
xmlhttp.open("GET","test1.txt",false);
我們不推薦使用 async=false,但是對于一些小型的請求,也是可以的。
請記住,JavaScript 會等到服務(wù)器響應(yīng)就緒才繼續(xù)執(zhí)行。如果服務(wù)器繁忙或緩慢,應(yīng)用程序會掛起或停止。
注釋:當您使用 async=false 時,請不要編寫 onreadystatechange 函數(shù) - 把代碼放到 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()">請求數(shù)據(jù)</button> <div id="myDiv"></div> </body> </html>
2_10.txt
這是通過AJAX得到的內(nèi)容?。?!
?
?
?