Asp. Net + Jquery. Ajax details 6-$. ajaxSetup

Source: Internet
Author: User

 


Directory (updated articles will be connected, and an article will be updated every two to three days starting from March 13, July 25 ):


Asp. Net + Jquery. Ajax details 1-opening section (published on February 25)

Asp. Net + Jquery. Ajax details 2-$. Load (published on 2012.07.26)

Asp. Net + Jquery. Ajax details 3-$. get and $. post (published on February 30)

Asp. Net + Jquery. Ajax details 4-$. getJSON (published on February 31)

Asp. Net + Jquery. Ajax details 5-$. getScript (2012.08.04)

Asp. Net + Jquery. Ajax details 6-$. ajaxSetup (2012.08.06)

Asp. Net + Jquery. Ajax details 7-Global Ajax events

Asp. Net + Jquery. Ajax details 8-core $. ajax

Asp. Net + Jquery. Ajax details 9-serialize and serializeArray

Asp. Net + Jquery. Ajax detailed description: 10-JSON and XML + written at the end

 

 

JQuery. ajaxSetup ([options])

 

Set global AJAX default options. If you want to set the default attributes of all jquery ajax functions on the interface, you can use this function to set options, the default options for all Ajax requests will follow the settings we set through this function.

 

Parameters

Options. All settings are optional.

 

It mainly uses the name/value (name: value) pair to specify the AJAX request settings.

 

Go to the instance first.

 

Client --

[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", // you can specify the default address of an AJAX request.
Data: {"param": "Jialin"}, // you can specify the default parameters for an AJAX request.
Global: false, // disable triggering of global AJAX events
Type: "POST", // use POST instead of the default GET Method
Success: function (responseText) {$ ("# result" ..html (responseText);} // sets the default callback function
});
 
// Bind button event
$ ("# 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 = "Use the global AJAX default option to call the get () method. The configured Request Method post is replaced."/>
<Input id = "testPost" type = "button" value = "Use the global AJAX default option to call the post () method"/>
<Input id = "testGet2" type = "button" value = "Pass Parameter override default parameter call get () method"/>
<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", // you can specify the default address of an AJAX request.
Data: {"param": "Jialin"}, // you can specify the default parameters for an AJAX request.
Global: false, // disable triggering of global AJAX events
Type: "POST", // use POST instead of the default GET Method
Success: function (responseText) {$ ("# result" ..html (responseText);} // sets the default callback function
});

// Bind button event
$ ("# 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 = "Use the global AJAX default option to call the get () method. The configured Request Method post is replaced."/>
<Input id = "testPost" type = "button" value = "Use the global AJAX default option to call the post () method"/>
<Input id = "testGet2" type = "button" value = "Pass Parameter override default parameter call get () method"/>
<Div id = "result"> </div>
</Div>
</Form>
</Body>
</Html>

Server --

[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 = "";
 
// Obtain parameters
If (! String. IsNullOrEmpty (HttpContext. Current. Request ["param"])
{
Param = HttpContext. Current. Request ["param"];
}

// Clear the buffer
Response. Clear ();
// Write the string to the response output stream
Response. Write ("Http Request Method:" + Request. HttpMethod. ToUpper () + "; the passed parameter is:" + param );
// Send the Current Buffer output to the client and stop executing this page
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 = "";

// Obtain parameters
If (! String. IsNullOrEmpty (HttpContext. Current. Request ["param"])
{
Param = HttpContext. Current. Request ["param"];
}

// Clear the buffer
Response. Clear ();
// Write the string to the response output stream
Response. Write ("Http Request Method:" + Request. HttpMethod. ToUpper () + "; the passed parameter is:" + param );
// Send the Current Buffer output to the client and stop executing this page
Response. End ();

}
}
}
 

 

Briefly,

The above Code sets the basic data required for an Ajax request: the request url, parameter, request type, and callback function after success.

Then we can use get () and post () without parameters to send ajax requests.

 

Note that when the get () or post () method is used, all other parameters except the type parameter will be rewritten to "GET" or "POST", use the default global option. if you change an option, such as the last url and parameter, the changed option prevails. if no option is changed, for example, the callback function still uses the Global option settings.

 

The global parameter indicates whether to trigger a global Ajax event.

 

Type: Boolean value. The default value is true.

 

Global Ajax events are a series of events that accompany Ajax requests. They mainly include the following events:

 

Execution function when ajaxComplete (callback) AJAX request is complete

AjaxError (callback) the execution function when an error occurs in an AJAX request

AjaxSend (callback) AJAX request execution function before sending

AjaxStart (callback) the function is executed when the AJAX request starts.

AjaxStop (callback) the function is executed when the AJAX request ends.

Execute the function when the ajaxSuccess (callback) AJAX request is successful.

 

For more information, see the next article.

Author: shan9liang
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.