3. 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":
如果你设置了请求的格式为"json",此时你没有设置Response回来的ContentType 为:Response.COntentType= "application/json"; 那么你将无法捕捉到返回的数据。 注意一下,alert(data.result); 由于设置了Accept报头为“json”,这里返回的data就是一个对象,并不需要用eval()来转换为对象。 4. jQuery.getScript( url, [callback] ) : 通过 GET 方式请求载入并执行一个 Javascript 文件。 参数 url (String) : 待载入 JS 文件地址。 callback (Function) : (可选) 成功载入后回调函数。 jQuery 1.2 版本之前,getScript 只能调用同域 JS 文件。 1.2中,您可以跨域调用 Javascript 文件。注意:Safari 2 或更早的版本不能在全局作用域中同步执行脚本。如果通过 getScript 加入脚本,请加入延时函数。 这个方法可以用在例如当只有编辑器focus()的时候才去加载编辑器需要的JS文件.下面看一些示例代码: 加载并执行 test.js。 jQuery 代码: $.getScript("test.js?6.1.3"); -------------------------------------------------------------------------------- 加载并执行 AjaxEvent.js ,成功后显示信息。 jQuery 代码: $.getScript("AjaxEvent.js?6.1.3", function(){ alert("AjaxEvent.js 加载完成并执行完成.你再点击上面的Get或Post按钮看看有什么不同?"); }); 加载完后请重新点击一下上面的 Load 请求看看有什么不同。 jQuery Ajax 事件 Ajax请求会产生若干不同的事件,我们可以订阅这些事件并在其中处理我们的逻辑。在jQuery这里有两种Ajax事件:局部事件 和 全局事件。 局部事件就是在每次的Ajax请求时在方法内定义的,例如: $.ajax({ beforeSend: function(){ // Handle the beforeSend event }, complete: function(){ // Handle the complete event } // ... });全局事件是每次的Ajax请求都会触发的,它会向DOM中的所有元素广播,在上面 getScript() 示例中加载的脚本就是全局Ajax事件。全局事件可以如下定义: $("#loading").bind("ajaxSend", function(){ $(this).show(); }).bind("ajaxComplete", function(){ $(this).hide(); });或者: $("#loading").ajaxStart(function(){ $(this).show(); }); 我们可以在特定的请求将全局事件禁用,只要设置下 global 选项就可以了: $.ajax({ url: "test.html", global: false,// 禁用全局Ajax事件. // ... });下面是jQuery官方给出的完整的Ajax事件列表: ajaxStart (Global Event) This event is broadcast if an Ajax request is started and no other Ajax requests are currently running. beforeSend (Local Event) This event, which is triggered before an Ajax request is started, allows you to modify the XMLHttpRequest object (setting additional headers, if need be.) ajaxSend (Global Event) This global event is also triggered before the request is run. success (Local Event) This event is only called if the request was successful (no errors from the server, no errors with the data). ajaxSuccess (Global Event) This event is also only called if the request was successful. error (Local Event) This event is only called if an error occurred with the request (you can never have both an error and a success callback with a request). ajaxError (Global Event) This global event behaves the same as the local error event. complete (Local Event) This event is called regardless of if the request was successful, or not. You will always receive a complete callback, even for synchronous requests. ajaxComplete (Global Event) This event behaves the same as the complete event and will be triggered every time an Ajax request finishes. ajaxStop (Global Event) This global event is triggered if there are no more Ajax requests being processed. 具体的全局事件请参考API文档。 好了,下面开始说jQuery里面功能最强的Ajax请求方法 $.ajax(); jQuery.ajax( options ) : 通过 HTTP 请求加载远程数据 这个是jQuery 的底层 AJAX 实现。简单易用的高层实现见 $.get, $.post 等。 $.ajax() 返回其创建的 XMLHttpRequest 对象。大多数情况下你无需直接操作该对象,但特殊情况下可用于手动终止请求。 注意: 如果你指定了 dataType 选项,请确保服务器返回正确的 MIME 信息,(如 xml 返回 "text/xml")。错误的 MIME 类型可能导致不可预知的错误。见 Specifying the Data Type for AJAX Requests 。 当设置 datatype 类型为 'script' 的时候,所有的远程(不在同一个域中)POST请求都回转换为GET方式。 $.ajax() 只有一个参数:参数 key/value 对象,包含各配置及回调函数信息。详细参数选项见下。 jQuery 1.2 中,您可以跨域加载 JSON 数据,使用时需将数据类型设置为 JSONP。使用 JSONP 形式调用函数时,如 "myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。数据类型设置为 "jsonp" 时,jQuery 将自动调用回调函数。(这个我不是很懂) 参数列表: