Jquery教程
Jquery动效
Jquery HTML/CSS
Jquery 事件
jquery常用

Jquery post()方法

post()方法是常用的HTTP方法之一。它用于使用HTTP POST请求从服务器加载页面。此方法从不缓存数据,通常用于与请求一起发送数据。我们无法为POST请求添加书签。

语法

$(selector).post(URL, data, function(data, status, xhr), dataType)

参数值

此方法包括四个参数值,其中一个是必需的,其他是可选的。我们必须在 post()方法中包含 URL 参数。下表中定义了 post()方法的参数值。
参数
URL 这是请求发送到的URL。这是必填参数。
data 这是一个可选参数。它是与请求一起发送到服务器的数据。
function(data, status, xhr), dataType) 这也是一个可选参数。它是在成功的服务器请求上执行的回调函数。
它还具有三个参数: data,status, xhr ,其中 data 包含来自服务器的结果数据, 状态 代表请求状态例如"成功","错误"等,并且 xhr 包含XMLHttpRequest对象。
dataType 这也是一个可选参数,用于定义我们期望服务器提供的数据类型。类型可以是"text"," json"," jsonp"," html","script"和" XML"。
现在,让我们来看一个使用 post()方法的简单示例。

示例

在在此示例中,我们使用 post()方法发出发布请求。我们使用两个参数,分别是 URL post()方法。在这里,Http发布请求用于从服务器加载数据。
URL参数设置为值 test.html 。回调函数具有两个参数 data status ,其中第一个参数保存所请求页面的数据,第二个参数保存请求状态。
在输出中,我们可以看到服务器加载的数据,请求的状态为 成功。
test.html
<h1> Hello World </h1>
<h2> Welcome to the lidihuo.com </h2>

Example2.html

<!DOCTYPE html>
<html>
<head>
<title> jQuery post() method </title>
<script src= "/ajax/libs/jquery/3.5.1/jquery.min.js"> </script>
</head>
<body>
<h3> This is an example of using the post() method in jQuery </h3>
<p> Click the following button to see the effect. </p>
<button> Click me </button>
<p id = "p1"> </p>
<p id = "p2"> </p>
<script>
$(document).ready(function() {
$("button").click(function() {
$.post("test.html", function(data,status) {
document.getElementById("p1").innerHTML = data;
document.getElementById("p2").innerHTML = "Status: " + status;
});
});
});
</script>
</body>
</html>
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4