当前位置:编程学习 > JS >>

jquery $.post同时处理多个ajax方法

jquery 1.5发布后,其中新增加方法jQuery.when().可以一次处理多个ajax请求。更多详细情况查看jquery api文档。
Collection by Ancker

jQuery.post( url, [data], [callback], [type] ) :使用POST方式来进行异步请求


参数:

url (String) : 发送请求的URL地址.

data (Map) : (可选) 要发送给服务器的数据,以 Key/value 的键值对形式表示。

callback (Function) : (可选) 载入成功时回调函数(只有当Response的返回状态是success才是调用该方法)。

type (String) : (可选)官方的说明是:Type of data to be sent。其实应该为客户端请求的类型(JSON,XML,等等)

 


这是一个简单的 POST 请求功能以取代复杂 $.ajax 。请求成功时可调用回调函数。如果需要在出错时执行函数,请使用 $.ajax。示例代码:

Ajax.asp教程x:

Response.ContentType = "application/json";
Response.Write("{result: '" + Request["Name"] + ",你好!(这消息来自服务器)'}");
jQuery 代码:
$.post("Ajax.aspx", { Action: "post", Name: "lulu" },
  function (data, textStatus){
   // data 可以是 xmlDoc, jsonObj, html, text, 等等.
   //this; // 这个Ajax请求的选项配置信息,请参考jQuery.get()说到的this
   alert(data.result);
  }, "json");

 

$(document).ready(function () {
$('#getsetgo').click(function () {
$.when($.ajax("page1.php教程"), $.ajax("do.php")).done(function(a1, a2){
$('#id1').html(a1[0]);
$('#id2').html(a2[0]);
});
});
});


jquery 同一个页面处理多个ajax请求的另外一种方法

$.post(
"do.php",
{
type: '1'
},
function(data, textStatus)
{
},
"json");
$.post(
"doSysthFile.aspx",
{
type: '2'
},
function(data, textStatus)
{
},
"json");

在php中处理简单

if( $_REquest['type'] )
{
  echo 1;exit;
}
echo 0;

补充:网页制作,js教程 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,