3. jQuery.post( url, [data], [callback], [type] ) :使用POST方式来进行异步请求 [indent]参数:
[b]url /b : 发送请求的URL地址.
[b]data /b : (可选) 要发送给服务器的数据,以 Key/value 的键值对形式表示。
[b]callback /b : (可选) 载入成功时回调函数(只有当Response的返回状态是success才是调用该方法)。
type (String) : (可选)官方的说明是:Type of data to be sent。其实应该为客户端请求的类型(JSON,XML,等等)
[/indent]这是一个简单的 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":
如果你设置了请求的格式为"json",此时你没有设置Response回来的ContentType 为:Response.ContentType = “application/json”; 那么你将无法捕捉到返回的数据。 注意一下,alert(data.result); 由于设置了Accept报头为“json”,这里返回的data就是一个对象,并不需要用eval()来转换为对象。