$.ajax、$.post[转]
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");
$.post("/user/CheckUserName", { userName: userName }, function (result) { alert(result) }, "text");
点击提交:
这里设置了请求的格式为"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"); }

随机推荐
- Codeforces Round #541 (Div. 2)D(并查集(dsu),拓扑排序)
#include<bits/stdc++.h>using namespace std;vector<int>g[2007];int fa[2007],vis[2007],num ...
- poj1753(高斯消元解mod2方程组)
题目链接:http://poj.org/problem?id=1753 题意:一个 4*4 的棋盘,初始时上面放满了黑色或白色的棋子.对 (i, j) 位置进行一次操作后 (i, j), (i + 1 ...
- loj #2025. 「JLOI / SHOI2016」方
#2025. 「JLOI / SHOI2016」方 题目描述 上帝说,不要圆,要方,于是便有了这道题. 由于我们应该方,而且最好能够尽量方,所以上帝派我们来找正方形.上帝把我们派到了一个有 NNN ...
- std::ostream_iterator用法
Defined in header <iterator> template< class T, class CharT = char, class Traits = std:: ...
- luogu3172 [CQOI2015]选数 莫比乌斯反演+杜教筛
link 题目大意:有N个数,每个数都在区间[L,H]之间,请求出所有数的gcd恰好为K的方案数 推式子 首先可以把[L,H]之间的数字gcd恰好为K转化为[(L-1)/K+1,H/K]之间数字gcd ...
- 改变iOS app的icon(iOS10.3)
原文 改变iOS app的icon官方iOS10.3新增了可以让开发者去更改app的icon,接下来看看怎么更改.官方API给的东西很少,只是介绍了一个实例方法: 1 open func setAlt ...
- HTTP Status 500 - org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
HTTP Status 500 - org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.e ...
- 页面加载完成前的loading加载效果
/*loading.js*/ // 加载HTML图 var _LoadingHtml = '<div id="loadingDiv" style="position ...
- UVA 10806
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> us ...
- mysql 中decimal中去掉后面多余的0
#去除Decimal后面多余的0 #处理前SELECT '0.12000','1.203010','-0.20' #处理后SELECT 0+CAST('0.12000' AS CHAR),0+CAST ...