您的位置:首页 > Web前端 > JQuery

jQuery $.post $.ajax用法

2012-01-16 02:51 423 查看
======================================================

注:本文源代码点此下载

======================================================

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.aspx:
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");
点击提交:
这里设置了请求的格式为"json":
$.ajax()这个是jquery 的底层 ajax 实现。简单易用的高层实现见 $.get, $.post 等。
这里有几个ajax事件参数:beforesend ,success ,complete ,error 。我们可以定义这些事件来很好的处理我们的每一次的ajax请求。
$.ajax({
url: 'stat.php',
type: 'post',
data:{name:"keyun"},
datatype: 'html',
timeout: 1000,
error: function(){alert('error loading php document');},
success: function(result){alert(result);}
});
//add by q at 2008.11.25
今天遇到一个jquery的post的小问题
因为要批量删除,所以开始用循环的post到那个url,然后刷新本页
这就出现问题了
$("input[@name='qa_checkbox']").each(function()
{
if($(this).attr('checked') == undefined)
{
}
else
{
$.post(url,{action:"post"},function(data){alert(data);window.location.reload();}, "text");
}
})
这么用的话 只能删除第一条数据;
$("input[@name='qa_checkbox']").each(function()
{
if($(this).attr('checked') == undefined)
{
}
else
{
$.post(url+$(this).val(),{action:"post"},function(data){alert(data);}, "text");
}
})
window.location.reload();
这样用的话,虽然可以删除,也能刷新本页,貌似reload是在post的function之前运行,但是post会报错,其中原因有待研究;
最终想了折中的办法
$("input[@name='qa_checkbox']").each(function()
{
if($(this).attr('checked') == undefined)
{
}
else
{
url = url + $(this).val() + '_';
}
})
$.post(url,{action:"post"},function(data){alert(data);window.location.reload();}, "text");
}
把要删除的id连成字符串,用一次post处理,把reload放在post的function里 就没有问题了
绿色通道:好文要顶关注我收藏该文与我联系



======================================================

在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定
这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: