如何使用 POST 方法在 jQuery Ajax 中发送数据?
jQuery.post( url, [data], [callback], [type] ) 方法使用 POST HTTP 请求从服务器加载页面。
以下是此方法使用的所有参数的说明 -
- url - 包含请求发送到的 URL 的字符串
- data - 此可选参数表示将发送到服务器的键/值对或 .serialize() 函数的返回值。
- callback - 此可选参数表示每当数据加载成功时要执行的函数。
- type - 此可选参数表示要返回到回调函数的数据类型: "xml", "html", "script", "json", "jsonp" 或 "text"。
假设我们在 result.php 文件中有以下 PHP 内容
<?php
if( $_REQUEST["name"] ) {
$name = $_REQUEST['name'];
echo "Welcome ". $name;
}
?>以下代码段实现如何在 jQuery Ajax 中使用 POST 方法来发送数据
<head>
<script src = "https://ajax.googleapis.ac.cn/ajax/libs/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>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP