JQuery. ajaxSetup () is a method in jquery ajax. I will introduce this method in some simple examples. I hope this article will be useful to you.
JQuery. ajaxSetup (options)
OptionsA set of key/value pairs that configure the default Ajax request. All options are optional.
Used to set detailed parameters for $. ajaxSetup (), see $. ajax ().
All subsequent Ajax calls to any function will use the new Setting Parameter unless it is specially requested until the next call $. ajaxSetup ().
For example, we can set the default URL parameters for repeated server responses in advance:
| The Code is as follows: |
Copy code |
$. AjaxSetup ({ Url: 'Ping. php ', }); |
Now this URL is automatically used for each Ajax request:
| The Code is as follows: |
Copy code |
$. Ajax ({ Data: {'name': 'time '}, }); |
Note: Global callback functions should use their respective global Ajax event handling methods -. ajaxStart (),. ajaxStop (),. ajaxComplete (),. ajaxError (),. ajaxSuccess (),. ajaxSend ()-set, instead of $. ajaxSetup () sets the settings object.
Example:
Set the default address of an AJAX request to "/xmlhttp/". Do not trigger a global AJAX event. Use POST instead of the default GET method. No option parameters are set for subsequent AJAX requests.
| The Code is as follows: |
Copy code |
$. AjaxSetup ({ Url: "/xmlhttp /", Global: false, Type: "POST" }); $. Ajax ({data: myData }); |
Example
| The Code is as follows: |
Copy code |
<! DOCTYPEhtmlPUBLIC "-// W3C // dtd xhtml 1.0 Transitional //" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <Htmlxmlns = "http://www.w3.org/1999/xhtml"> <Head> <Title> jQuery Ajax-Load </title> <Scripttype = "text/javascript" src = "../scripts/jquery-1.3.2-vsdoc2.js"> </script> <Script type = "text/javascript"> $ (Document). ready (function () { $. AjaxSetup ({ Url: "../data/AjaxGetMethod. aspx ", Data: {"param": "ziqiu. zhang "}, Global: false, Type: "POST ", Success: function (data, textStatus) {$ ("# divResult" ).html (data );} }); $ ("# BtnAjax"). click (function (event) {.. ajax ();}); $ ("# BtnGet"). click (function (event) {$. get ();}); $ ("# BtnPost"). click (function (event) {$. post ();}); $ ("# BtnGet2 "). click (function (event) {$. get (".. /data/AjaxGetMethod. aspx ", {" param ":" other "});}); }); </Script> </Head> <Body> <Buttonid = "btnAjax"> call the ajax () method without passing parameters </button> <br/> <Buttonid = "btnGet"> call the get () method without passing parameters </button> <br/> <Buttonid = "btnPost"> call the post () method without passing parameters </button> <br/> <Buttonid = "btnGet2"> call the get () method by passing parameters and use the global default callback function </button> <br/> <Br/> <Divid = "divResult"> </div> </Body> </Html> |
Note that when the get () or post () method is used, except that the type parameter is overwritten as "GET" or "POST, if other parameters are not passed, the default global option is used. if a certain option is passed, for example, the url and parameter are passed in the last button, the passed option prevails in this call. if no option is passed, for example, the callback function still uses the Global option to set the value.