目录

jQuery.post( url, [data], [callback], [type] )

描述 (Description)

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

该方法返回XMLHttpRequest对象。

语法 (Syntax)

以下是使用此方法的简单语法 -

<i>$</i>.post( url, [data], [callback], [type] )

参数 (Parameters)

以下是此方法使用的所有参数的说明 -

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

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

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

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

例子 (Example)

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

<?php
if( $_REQUEST["name"] ) {
   $name = $_REQUEST['name'];
   echo "Welcome ". $name;
}
?>

以下是一个简单的例子,简单地显示了这种方法的用法 -

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("#driver").click(function(event){
               $.post( 
                  "result.php",
                  { name: "Zara" },
                  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>
</html>

这应该产生以下结果 -

新页面打开
↑回到顶部↑
WIKI教程 @2018