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

来源:岁月联盟 编辑:exp 时间:2012-08-06

 


目录(已经更新的文章会有连接,从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事件

Asp.Net+Jquery.Ajax详解8-核心$.ajax

Asp.Net+Jquery.Ajax详解9-serialize和serializeArray

Asp.Net+Jquery.Ajax详解10-JSON和XML+写在最后

 

 

jQuery.ajaxSetup([options])

 

设置全局 AJAX 默认选项。如果我们要设置界面上所有的jquery ajax 函数的默认属性,就可以使用该函数设置options选项, 此后所有的Ajax请求的默认options将遵循我们通过该函数设置的.

 

参数

options 选项设置。所有设置项均为可选设置。

 

主要是使用名称/值(name:value)对来规定 AJAX 请求的设置。

 

先上实例,我们再来说明

 

客户端——

[html] 
<%@ 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> 

<%@ 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>

服务端——

[csharp] 
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(); 
 
        } 
    } 

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 请求成功时执行函数

 

具体的,我们下一篇再介绍.

 作者:shan9liang