jQuery - AJAX get() 和 post() 方法
HTTP 請求:GET vs. POST
兩種在客戶端和伺服器端進行請求-回應的常用方法是:GET 和 POST。
GET?- 從指定的資源請求資料POST?- 向指定的資源提交要處理的資料
GET 基本上用於從伺服器取得(取回)資料。註:GET 方法可能會傳回快取資料。
POST 也可用於從伺服器取得資料。不過,POST 方法不會快取數(shù)據(jù),而且常用於連同請求一起發(fā)送數(shù)據(jù)。
Get和Post的差異
##Get方式:用get方式可傳送簡單數(shù)據(jù),但大小一般限制在1KB下,數(shù)據(jù)追加到url中發(fā)送(http的header傳送),也就是說,瀏覽器將各個表單字段元素及其數(shù)據(jù)按照URL參數(shù)的格式附加在請求行中的資源路徑後面。另外最重要的一點是,它會被客戶端的瀏覽器快取起來,那麼,別人就可以從瀏覽器的歷史記錄中,讀取到此客戶的數(shù)據(jù),例如帳號和密碼等。因此,在某些情況下,get方法會帶來嚴重的安全性問題。
Post方式:
當使用POST方式時,瀏覽器會把各表單欄位元素及其資料當作HTTP訊息的實體內容傳送給Web伺服器,而不是作為URL位址的參數(shù)傳遞,使用POST方式傳遞的資料量要比使用GET方式傳送的資料量大的多。
$.get(URL,callback);
必要的?URL參數(shù)規(guī)定您希望請求的URL。 可選的?callback?參數(shù)是請求成功後執(zhí)行的函數(shù)名稱。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $.get("/try/ajax/demo_test.php",function(data,status){ //需要引入demo_test.php文件 alert("數(shù)據(jù): " + data + "\n狀態(tài): " + status); }); }); }); </script> </head> <body> <button>發(fā)送一個GET 請求并獲取返回結果</button> </body> </html>
$.get() 的第一個參數(shù)是我們希望請求的 URL("demo_test.php")。
第二個參數(shù)是回呼函數(shù)。第一個回呼參數(shù)存有被請求頁面的內容,第二個回呼參數(shù)存有請求的狀態(tài)。
這個PHP 檔案("demo_test.php") 類似這樣:
<?php echo "這是個從PHP文件中讀取的數(shù)據(jù)"; ?>
$.post() 方法
$.post() 方法透過HTTP POST 請求從伺服器上請求資料。
語法:
$.post(URL,data,callback);
必需的?URL?參數(shù)規(guī)定您希望請求的URL。
可選的?data?參數(shù)規(guī)定連同請求發(fā)送的資料。
可選的?callback?參數(shù)是請求成功後執(zhí)行的函數(shù)名稱。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $.post("/try/ajax/demo_test_post.php",{ name:"php中文網(wǎng)", url:"http://miracleart.cn" }, function(data,status){ alert("數(shù)據(jù): \n" + data + "\n狀態(tài): " + status); }); }); }); </script> </head> <body> <button>發(fā)送一個 HTTP POST 請求頁面并獲取返回內容</button> </body> </html>
$.post() 的第一個參數(shù)是我們想要請求的 URL ("demo_test_post.php")。
然後我們連同請求(name 和 city)一起發(fā)送資料。
"demo_test_post.php" 中的 PHP 腳本讀取這些參數(shù),對它們進行處理,然後傳回結果。
第三個參數(shù)是回呼函數(shù)。第一個回呼參數(shù)存有被請求頁面的內容,而第二個參數(shù)存有請求的狀態(tài)。
這個PHP 檔案("demo_test_post.php") 類似這樣:
<?php $name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ''; $city = isset($_POST['url']) ? htmlspecialchars($_POST['url']) : ''; echo '網(wǎng)站名: ' . $name; echo "\n"; echo 'URL 地址: ' .$city; ?>
一個完整的$.post()實例:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> function checkname(){ if($('#name').val() == ""){ $('#msg').html("please enter the name!"); $('#name').focus; return false; } if($('#address').val() == ""){ $('#msg').html("please enter the address!"); $('#address').focus; return false; } ajax_post(); } function ajax_post(){ $.post("text.php",{name:$('#name').val(),address:$('#address').val()}, function(data){ //$('#msg').html("please enter the name!"); //alert(data); $('#msg').html(data); }, "text"); } </script> </head> <body> <form id="ajaxform" name="ajaxform" method="post" action="text.php"> <p> name<input type="text" name="name" id="name"/> </p> <p> address<input type="text" name="address" id="address"/> </p> <p id="msg"></p> <p> <input name="Submit" type="button" value="submit" onclick="return checkname()"/> </p> </form> </body> </html>
建立一個text.php的檔案:
<?php $name = $_POST["name"]; $address = $_POST["address"]; echo $name."<br>"; echo $address."<br>"; echo "success"; ?>