jquery $.ajax相關(guān)用法分享
更新時(shí)間:2012年03月16日 21:19:32 作者:
jquery $.ajax相關(guān)用法分享,使用jquery的朋友可以參考下
復(fù)制代碼 代碼如下:
$.ajax({
type: "GET",
url: "Services/EFService.svc/Members",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
// Play with returned data in JSON format
},
error: function (msg) {
alert(msg);
}
});
全局事件是每次的Ajax請求都會觸發(fā)的,它會向DOM中的所有元素廣播,在上面 getScript() 示例中加載的腳本就是全局Ajax事件。全局事件可以如下定義:
復(fù)制代碼 代碼如下:
$("#loading").ajaxStart(function(){
$(this).show();
});
我們可以在特定的請求將全局事件禁用,只要設(shè)置下 global 選項(xiàng)就可以了:
復(fù)制代碼 代碼如下:
$.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.
jQuery.ajaxSetup( options ) : 設(shè)置全局 AJAX 默認(rèn)選項(xiàng)。
設(shè)置 AJAX 請求默認(rèn)地址為 "/xmlhttp/",禁止觸發(fā)全局 AJAX 事件,用 POST 代替默認(rèn) GET 方法。其后的 AJAX 請求不再設(shè)置任何選項(xiàng)參數(shù)。
jQuery 代碼:
復(fù)制代碼 代碼如下:
$.ajaxSetup({
url: "/xmlhttp/",
global: false,
type: "POST"
});
$.ajax({ data: myData });
如果指定為 html 類型,任何內(nèi)嵌的 JavaScript 都會在 HTML 作為一個(gè)字符串返回之前執(zhí)行。類似地,指定 script 類型的話,也會先執(zhí)行服務(wù)器端生成 JavaScript,然后再把腳本作為一個(gè)文本數(shù)據(jù)返回。
JSON 數(shù)據(jù)是一種能很方便通過 JavaScript 解析的結(jié)構(gòu)化數(shù)據(jù)。如果獲取的數(shù)據(jù)文件存放在遠(yuǎn)程服務(wù)器上(域名不同,也就是跨域獲取數(shù)據(jù)),則需要使用 jsonp 類型。使用這種類型的話,會創(chuàng)建一個(gè)查詢字符串參數(shù) callback=? ,這個(gè)參數(shù)會加在請求的 URL 后面。服務(wù)器端應(yīng)當(dāng)在 JSON 數(shù)據(jù)前加上回調(diào)函數(shù)名,以便完成一個(gè)有效的 JSONP 請求。如果要指定回調(diào)函數(shù)的參數(shù)名來取代默認(rèn)的 callback,可以通過設(shè)置 $.ajax() 的 jsonp 參數(shù)。
您可能感興趣的文章:
- javascript jQuery $.post $.ajax用法
- 超簡單的jquery的AJAX用法
- JQuery的ajax的用法在asp中使用$.ajax()實(shí)現(xiàn)
- Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法總結(jié)
- js jquery ajax的幾種用法總結(jié)(及優(yōu)缺點(diǎn)介紹)
- Jquery中ajax方法data參數(shù)的用法小結(jié)
- jQuery+ajax中g(shù)etJSON() 用法實(shí)例
- jQuery中ajax的post()方法用法實(shí)例
- 談?wù)刯Query Ajax用法詳解
相關(guān)文章
jQuery實(shí)現(xiàn)帶有洗牌效果的動畫分頁實(shí)例
這篇文章主要介紹了jQuery實(shí)現(xiàn)帶有洗牌效果的動畫分頁,涉及jquery頁面元素樣式及animate方法的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08jQuery實(shí)現(xiàn)的多級下拉菜單效果代碼
這篇文章主要介紹了jQuery實(shí)現(xiàn)的多級下拉菜單效果代碼,涉及jquery鼠標(biāo)事件及頁面元素的顯示與隱藏效果實(shí)現(xiàn)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-08-08基于jquery實(shí)現(xiàn)智能表單驗(yàn)證操作
這篇文章主要為大家詳細(xì)介紹了基于jquery實(shí)現(xiàn)智能表單驗(yàn)證操作的相關(guān)資料,利用jquery實(shí)現(xiàn)了對一個(gè)表單字段格式的即時(shí)檢查,感興趣的小伙伴們可以參考一下2016-05-05ExtJs動態(tài)生成treepanel的Json格式
這篇文章主要介紹了ExtJs動態(tài)生成treepanel的Json格式的相關(guān)資料,需要的朋友可以參考下2015-07-07jQuery+PHP實(shí)現(xiàn)動態(tài)數(shù)字展示特效
我們在一些應(yīng)用中需要動態(tài)展示數(shù)據(jù),比如當(dāng)前在線人數(shù),當(dāng)前交易總額,當(dāng)前匯率等等,前端頁面需要實(shí)時(shí)刷新獲取最新數(shù)據(jù)。本文將結(jié)合實(shí)例給大家介紹使用jQuery和PHP來實(shí)現(xiàn)動態(tài)數(shù)字展示效果。2015-03-03jquery+ajax實(shí)現(xiàn)注冊實(shí)時(shí)驗(yàn)證實(shí)例詳解
這篇文章主要介紹了jquery+ajax實(shí)現(xiàn)注冊實(shí)時(shí)驗(yàn)證的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了jQuery基于ajax請求實(shí)現(xiàn)注冊時(shí)無刷新驗(yàn)證的相關(guān)技巧,需要的朋友可以參考下2015-12-12