jquery $.ajax相關用法分享

來源:互聯網
上載者:User

複製代碼 代碼如下:$.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請求都會觸發的,它會向DOM中的所有元素廣播,在上面 getScript() 樣本中載入的指令碼就是全域Ajax事件。全域事件可以如下定義: 複製代碼 代碼如下:$("#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.
jQuery.ajaxSetup( options ) : 設定全域 AJAX 預設選項。
設定 AJAX 請求預設地址為 "/xmlhttp/",禁止觸發全域 AJAX 事件,用 POST 代替預設 GET 方法。其後的 AJAX 請求不再設定任何選項參數。
jQuery 代碼: 複製代碼 代碼如下:$.ajaxSetup({
url: "/xmlhttp/",
global: false,
type: "POST"
});
$.ajax({ data: myData });

如果指定為 html 類型,任何內嵌的 JavaScript 都會在 HTML 作為一個字串返回之前執行。類似地,指定 script 類型的話,也會先執行伺服器端產生 JavaScript,然後再把指令碼作為一個文本資料返回。
JSON 資料是一種能很方便通過 JavaScript 解析的結構化資料。如果擷取的資料檔案存放在遠程伺服器上(網域名稱不同,也就是跨域擷取資料),則需要使用 jsonp 類型。使用這種類型的話,會建立一個查詢字串參數 callback=? ,這個參數會加在請求的 URL 後面。伺服器端應當在 JSON 資料前加上回呼函數名,以便完成一個有效 JSONP 請求。如果要指定回呼函數的參數名來取代預設的 callback,可以通過設定 $.ajax() 的 jsonp 參數。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.