Asp.Net+Jquery.Ajax詳解7-全域Ajax事件

來源:互聯網
上載者:User

 

目錄(已經更新的文章會有串連,從7月25日開始,每2到3天更新一篇):

Asp.Net+Jquery.Ajax詳解1-開篇(2012.07.25發)

Asp.Net+Jquery.Ajax詳解2-$.Load(2012.07.26發)

Asp.Net+Jquery.Ajax詳解3-$.get和$.post(2012.07.30發)

Asp.Net+Jquery.Ajax詳解4-$.getJSON(2012.07.31發)

Asp.Net+Jquery.Ajax詳解5-$.getScript(2012.08.04發)

Asp.Net+Jquery.Ajax詳解6-$.ajaxSetup(2012.08.06發)

Asp.Net+Jquery.Ajax詳解7-全域Ajax事件(2012.08.09發)

Asp.Net+Jquery.Ajax詳解8-核心$.ajax(2012.08.12發)

Asp.Net+Jquery.Ajax詳解9-serialize和serializeArray(2012.08.15發)

Asp.Net+Jquery.Ajax詳解10-JSON和XML+寫在最後(2012.08.20發,結束啦!)

 

全域Ajax事件是一系列伴隨Ajax請求發生的事件.

 

主要有如下事件:

ajaxComplete( callback ) AJAX 請求完成時執行函數

ajaxError( callback ) AJAX 請求發生錯誤時執行函數

ajaxSend( callback ) AJAX 請求發送前執行函數

ajaxStart( callback ) AJAX 請求開始時執行函數

ajaxStop( callback ) AJAX 請求結束時執行函數

ajaxSuccess( callback ) AJAX 請求成功時執行函數

 

 

在學習$.ajaxSetup時,我們知道預設options的global屬性為true,代表發送ajax請求時,將觸發這些全域事件。

我們可以通過$.ajaxSetup將預設options的global屬性設定為false來取消全域Ajax事件的觸發.

 

這些事件(除去ajaxStart和ajaxStop)的回呼函數都包含3個參數

event - 包含 event 對象

xhr - 包含 XMLHttpRequest 對象

options - 包含 AJAX 請求中使用的選項

 

 我們通過一個執行個體來說明這些事件在什麼時候發生。這樣大家理解起來更直觀一些。

執行個體——用戶端

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JqueryAjaxGlobalEvent.aspx.cs" Inherits="JqueryAjaxTest.JqueryAjaxGlobalEvent" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title>jquery ajax test</title>    <script src="Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>     <script type="text/javascript">         $(function () {             //綁定按鈕事件             $("#TestAjaxEvent").click(function (event) { $.get("Data/GetServiceInfo.aspx"); });             $("#result").ajaxSend(function (event, xhr, options) { $(this).append('<p>ajaxSend</p>'); });             $("#result").ajaxStart(function () { $(this).append('<p>ajaxStart</p>'); });             $("#result").ajaxStop(function () { $(this).append('<p>ajaxStop</p>'); });             $("#result").ajaxComplete(function (event, xhr, options) { $(this).append('<p>ajaxComplete</p>'); });             $("#result").ajaxSuccess(function (event, xhr, options) { $(this).append('<p>ajaxSuccess</p>'); });             $("#result").ajaxError(function (event, xhr, options) { $(this).append('<p>ajaxError</p>'); });         });    </script></head><body>    <form id="form1" runat="server">    <div>       <input id="TestAjaxEvent" type="button" value="測試ajax全域事件發生順序" />       <div id="result">從上到下依次發生:</div>    </div>    </form></body></html>

服務端——

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace JqueryAjaxTest.Data{    public partial class GetMethodInfo : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            string param = "";            //擷取參數            if (!String.IsNullOrEmpty(HttpContext.Current.Request["param"]))            {                param = HttpContext.Current.Request["param"];            }                        //清空緩衝區            Response.Clear();            //將字串寫入響應輸出資料流            Response.Write("Http請求的方式為:"+Request.HttpMethod.ToUpper()+"; 傳遞過來的參數為:"+param);            //將當前所有緩衝的輸出發送的用戶端,並停止該頁執行            Response.End();        }    }}

注意ajaxSuccess和ajaxComplete的區別:

ajaxSuccess: 當請求成功時觸發該事件,回呼函數會得到參數,一個參數的屬性status==200。

ajaxComplete:當請求完成時觸發該事件,回呼函數會得的這個屬性status==404、403、302...。與 ajaxSuccess() 不同,通過 ajaxComplete() 方法規定的函數會在請求完成時運行,即使請求並未成功。

 

我們再原生的AJax中常常這麼寫,

 xmlhttp.onreadystatechange=function()

  {

  if (xmlhttp.readyState==4 && xmlhttp.status==200)

    {

    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

    }

  }

當 readyState 等於 4 且狀態為 200 時,表示響應已就緒。當status等於200時,也就是我們的ajaxsuccess事件觸發的時刻。

 

啊,這個系列文章寫到7啦,還有3篇,繼續加油!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.