如何使用POST方法在jQuery Ajax中发送数据?

jQuery.post(url,[data],[callback],[type])方法使用POST HTTP请求从服务器加载页面。

这是此方法使用的所有参数的描述-

  • url- 包含请求发送到的URL的字符串

  • 数据 -此可选参数表示的键/值对或将要发送到服务器的.serialize()函数的返回值。

  • callback- 此可选参数表示每当成功加载数据时将执行的函数。

  • type- 此可选参数表示要返回给回调函数的数据类型:“ xml”,“ html”,“ script”,“ json”,“ jsonp”或“ text”。

假设我们在result.php文件中具有以下PHP内容:

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

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

这是实现如何使用POST方法在jQuery Ajax中发送数据的代码段:

   <head>
      <script src = "https://cdn.staticfile.org/jquery/2.1.3/jquery.min.js"></script>
       
      <script>
         $(document).ready(function() {
           
            $("#driver").click(function(event){
               
               $.post(
                  "result.php",
                  { name: "John" },
                  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>