jQuery 中的 jQuery.post() 和 jQuery.get() 方法有什麼區別?


jQuery post() 方法

jQuery.post( url, [data], [callback], [type] ) 方法使用 POST HTTP 請求從伺服器載入頁面。

假設我們在 result.php 檔案中有以下 PHP 內容,

示例

以下程式碼段演示了此方法的使用 −

   <head>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
       
      <script>
         $(document).ready(function() {
           
            $("#driver").click(function(event){
               
               $.post(
                  "result.php",
                  { name: "Ricky" },
                  function(data) {
                     $('#stage').html(data);
                  }
               );      
            });
         });
      </script>
   </head>
   
   <body>
   
      <p>Click on the button to load result.html file −</p>
       
      <div id = "stage" style = "background-color:cc0;">
         STAGE
      </div>
       
      <input type = "button" id = "driver" value = "Load Data" />
       
   </body>

jQuery get() 方法

jQuery.get( url, [data], [callback], [type] ) 方法使用 GET HTTP 請求從伺服器載入資料。

假設我們在  result.php 檔案中有以下 PHP 內容 −

<?php
if( $_REQUEST["name"] ) {

   $name = $_REQUEST['name'];
   echo "Welcome ". $name;
}
?>

示例

以下程式碼段演示了此方法的使用 −

   <head>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
       
      <script>
         $(document).ready(function() {
           
            $("#driver").click(function(event){
               $.get(
                  "result.php",
                  { name: "John" },
                  function(data) {
                     $('#stage').html(data);
                  }
               );
            });
               
         });
      </script>    
   </head>    
   <body>
   
      <p>Click on the button to load result.html file</p>
       
      <span id = "stage" style = "background-color:#cc0;">
         STAGE
      </span>
       
      <div><input type = "button" id = "driver"
         value = "Load Data" /></div>
   
   </body>    


更新時間:17-Feb-2020

175 次檢視

Kickstart Your Career

透過完成課程獲得認證

開始
廣告