jQuery post()方法用法示例介绍

2021年4月12日11:42:51 发表评论 889 次浏览

post()方法jQuery中的服务器使用POST HTTP请求从服务器加载页面并返回XMLHttpRequest目的。

语法如下:

$.post( url, data, callback_function, data_type )

参数:此方法接受上述和以下所述的四个参数:

  • 网址:它是必需的参数, 用于发送请求。
  • 数据:它是可选参数, 代表将要发送到服务器的数据的键/值对。
  • callback_function:它是可选参数, 代表成功加载数据后要执行的功能。
  • 数据类型:它是可选参数, 代表要返回给回调函数的数据类型:xml, html, 脚本, json, jsonp或text。

main.php在下面的示例中, 当按下按钮时, 将调用此PHP文件。

<?php
     echo "Hello Geeks!" ;
?>

例子:本示例使用post()方法并调用一个PHP文件。

<!DOCTYPE html>
<html>
      
<head>
     <title>
         jQuery post() Method
     </title>
      
     <script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
     </script>
</head>
  
<body>
     <h2 id = "gfg">lsbin</h2> 
      
     <button id = "b">
         Click Here!
     </button> 
      
     <!-- Script to use post() method -->
     <script>
         $(document).ready(function() {
             $("button").click(function() {
                 $.post("/submit.php", {
                     name: "GFG", }, function(data, status) {
                     document.getElementById("gfg").innerHTML
                             = data;
                     document.getElementById("b").innerHTML
                             = "Data Passed";
                 });
             });
         });
     </script>
</body>
  
</html>

输出如下:

在单击按钮之前:

jQuery | post()方法1

单击按钮后:

jQuery | post()方法2

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: