Asp.Net+Jquery.Ajax詳解6-$.ajaxSetup

來源:互聯網
上載者: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發,結束啦!)

 

 

jQuery.ajaxSetup([options])

 

設定全域 AJAX 預設選項。如果我們要設定介面上所有的jquery ajax 函數的預設屬性,就可以使用該函數設定options選項, 此後所有的Ajax請求的預設options將遵循我們通過該函數設定的.

 

參數

options 選項設定。所有設定項均為可選設定。

 

主要是使用名稱/值(name:value)對來規定 AJAX 請求的設定。

 

先上執行個體,我們再來說明

 

用戶端——

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JqueryAjaxSetup.aspx.cs" Inherits="JqueryAjaxTest.JqueryAjaxSetup" %><!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 () {             $.ajaxSetup({                 url: "data/GetServiceInfo.aspx", //設定AJAX 請求預設地址                 data: { "param": "Jialin" }, //設定AJAX 請求預設參數                 global: false, //禁止觸發全域 AJAX 事件                 type: "POST", //用 POST 代替預設 GET 方法                 success: function (responseText) { $("#result").html(responseText); }//設定預設回呼函數             });             //綁定按鈕事件             $("#testGet").click(function (event) { $.get(); });             $("#testPost").click(function (event) { $.post(); });             $("#testGet2").click(function (event) { $.get("data/GetServiceInfo.aspx", { "param": "Jingquan" }); });         });    </script></head><body>    <form id="form1" runat="server">    <div>        <input id="testGet" type="button" value="採用全域 AJAX 預設選項調用get()方法,會替換掉設定的請求方式post" />           <input id="testPost" type="button" value="採用全域 AJAX 預設選項調用post()方法" />              <input id="testGet2" type="button" value="傳遞參數覆蓋預設參數調用get()方法" />       <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();        }    }}

 

 

簡要說明一下,

上面的代碼設定了一個Ajax請求需要的基本資料: 請求url, 參數, 請求類型, 成功後的回呼函數.

此後我們可以使用無參數的get(), post()發送ajax請求.

 

注意當使用get()或者post()方法時, 除了type參數將被重寫為"GET"或者"POST"外, 其他參數都是使用預設的全域option. 如果更改了某一個選項, 例如最後一個更改了url和參數, 則該次調用會以更改的選項為準. 沒有更改的選項比如回呼函數還是會使用全域option設定.

 

其中有一個參數global,用以表示是否觸發全域的Ajax事件.

 

類型:布爾值,預設為 true

 

全域Ajax事件是一系列伴隨Ajax請求發生的事件.主要有如下事件:

 

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

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

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

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

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

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

 

具體的,我們下一篇再介紹.

相關文章

聯繫我們

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