发送异步 http POST 请求以从服务器加载数据,其一般形式为:

jQuery.post( url [, data ] [, success ] [, dataType ] )
  • url:是唯一的必需参数。此字符串包含向其发送请求的地址。如果未指定其他参数,则返回的数据将被忽略。
  • data:与请求一起发送到服务器的对象或字符串。
  • success:如果请求成功,将执行这个回调函数。它将返回的数据作为参数,它还传递了响应的文本状态。
  • dataType:服务器期望的数据类型,默认值为 Intelligent Guess(xml、json、脚本、文本、html)。如果提供了此参数,则还必须提供 success 回调。

示例

$.post('http://example.com/form.php', {category:'client', type:'premium'});

从服务器请求 form.php,发送其他数据并忽略返回的结果。

$.post('http://example.com/form.php', {category:'client', type:'premium'}, function(response){ 
      alert("success");
      $("#mypar").html(response.amount);
});

从服务器请求 form.php,发送其他数据并处理返回的响应(json 格式)。该示例可以用以下格式编写:

$.post('http://example.com/form.php', {category:'client', type:'premium'}).done(function(response){
      alert("success");
      $("#mypar").html(response.amount);
});

以下示例使用 Ajax 发布表单并将结果放入 div 中:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery.post demo</title>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<form action="/" id="searchForm">
  <input type="text" name="s" placeholder="Search...">
  <input type="submit" value="Search">
</form>
<!-- 搜索结果将在这个 div 中渲染 -->
<div id="result"></div>
 
<script>
// 将提交处理程序附加到表单
$( "#searchForm" ).submit(function( event ) {
 
  // 阻止表格正常提交
  event.preventDefault();
 
  // 从页面元素中获取一些值:
  var $form = $( this ),
    term = $form.find( "input[name='s']" ).val(),
    url = $form.attr( "action" );
 
  // 用 post 发送数据
  var posting = $.post( url, { s: term } );
 
  // 将结果放在 div 中
  posting.done(function( data ) {
    var content = $( data ).find( "#content" );
    $( "#result" ).empty().append( content );
  });
});
</script>
 
</body>
</html>

以下示例使用 GitHub API,通过使用 jQuery.ajax() 获取用户的仓库列表,并将结果放入 div 中:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery Get demo</title>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<form id="userForm">
  <input type="text" name="username" placeholder="Enter gitHub User name">
  <input type="submit" value="Search">
</form>
<!-- 搜索结果将在这个 div 中渲染 -->
<div id="result"></div>
 
<script>
// 将提交处理程序附加到表单
$( "#userForm" ).submit(function( event ) {
 
  // 阻止表单正常提交
  event.preventDefault();
 
  // 从页面元素中获取一些值:
  var $form = $( this ),
    username = $form.find( "input[name='username']" ).val(),
    url = "https://api.github.com/users/"+username+"/repos";
 
  // 用 post 发送数据
  var posting = $.post( url, { s: term } );
 
  //Ajax 函数,发送 get 请求
  $.ajax({
    type: "GET",
    url: url,
    dataType:"jsonp"
    success: function(response){
        //如果请求成功,则响应数据

        $( "#result" ).empty().append( response );
    }
  });
  
});
</script>
 
</body>
</html>

jQuery.ajax()

$.post( url [, data ] [, success ] [, dataType ] ) 是一个简写的 Ajax 函数,等同于:

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

$.ajax() 还有更多选项,可点击这里查看。

更多信息

请在官网查看更多信息。

原文:JQuery Ajax POST Method