jQuery.ajaxSetup()
The function is used to set the global default settings of AJAX.
This function is used to change the default setting options for AJAX requests in jQuery. All AJAX requests executed thereafter will use the changed default settings if the corresponding option parameter is not set.
This function belongs to the global jQuery
object.
jQuery 1.1 Add the static function.
jQuery.ajaxSetup( settings )
Parameters | Description |
---|---|
settings | Object class is an object in which each attribute represents an option that needs to change the default settings, and the attribute value represents the changed default value. |
For the properties that can be identified by the settings
parameter, please refer to the settings parameter description in jQuery.ajax()
. All option parameters are optional.
Possible names/values are listed in the table below:
Name | Value/Description |
---|---|
async | Boolean value indicating whether the request is processed asynchronously. The default is true. |
beforeSend(xhr) | Function to run before sending the request. |
cache | Boolean value indicating whether the browser caches the requested page. The default is true. |
complete(xhr,status) | Function that runs when the request is completed (called after the request succeeds or fails, that is, after success and error function). |
contentType | The content type used when sending data to the server. The default is: "application/x-www-form-urlencoded". |
context | Specifies the "this" value for all AJAX-related callback functions. |
data | Specifies the data to be sent to the server. |
dataFilter(data,type) | Function used to process XMLHttpRequest raw response data. |
dataType | The data type of the expected server response. |
error(xhr,status,error) | Function to run if the request fails. |
global | Boolean value, specifies whether to trigger the global AJAX event handler program for the request. The default is true. |
ifModified | Boolean value that specifies whether the request succeeds only if the response has changed since the last request. The default is false. |
jsonp | Override the string of the callback function in a jsonp. |
jsonpCallback | Specifies the name of the callback function in a jsonp. |
password | Specifies the password used in HTTP access authentication requests. |
processData | Boolean value that specifies whether the data sent through the request is converted into a query string. The default is true. |
scriptCharset | Specifies the requested character set. |
success(result,status,xhr) | Function that runs when the request succeeds. |
timeout | Set the local request timeout (in milliseconds). |
traditional | Boolean value that specifies whether to use the traditional style of parameter serialization. |
type | Specifies the type of request (GET or POST). |
url | Specifies the URL to send the request. The default is the current page. |
username | Specifies the username used in HTTP access authentication requests. |
xhr | Function used to create XMLHttpRequest objects. |
jQuery.ajaxSetup()
The function has no return value, or its return value is undefined
.
<span class="com">//设置AJAX的全局默认选项<span class="pln"><br/>$<span class="pun">.<span class="pln">ajaxSetup<span class="pun">(<span class="pln"> <span class="pun">{<span class="pln"><br/> url<span class="pun">:<span class="pln"> <span class="str">"/index.html"<span class="pln"> <span class="pun">,<span class="pln"> <span class="com">// 默认URL<span class="pln"><br/> aysnc<span class="pun">:<span class="pln"> <span class="kwd">false<span class="pln"> <span class="pun">,<span class="pln"> <span class="com">// 默认同步加载<span class="pln"><br/> type<span class="pun">:<span class="pln"> <span class="str">"POST"<span class="pln"> <span class="pun">,<span class="pln"> <span class="com">// 默认使用POST方式<span class="pln"><br/> headers<span class="pun">:<span class="pln"> <span class="pun">{<span class="pln"> <span class="com">// 默认添加请求头<span class="pln"><br/> <span class="str">"Author"<span class="pun">:<span class="pln"> <span class="str">"CodePlayer"<span class="pln"> <span class="pun">,<span class="pln"><br/> <span class="str">"Powered-By"<span class="pun">:<span class="pln"> <span class="str">"CodePlayer"<span class="pln"><br/> <span class="pun">}<span class="pln"> <span class="pun">,<span class="pln"><br/> error<span class="pun">:<span class="pln"> <span class="kwd">function<span class="pun">(<span class="pln">jqXHR<span class="pun">,<span class="pln"> textStatus<span class="pun">,<span class="pln"> errorMsg<span class="pun">){<span class="pln"> <span class="com">// 出错时默认的处理函数<span class="pln"><br/> <span class="com">// jqXHR 是经过jQuery封装的XMLHttpRequest对象<span class="pln"><br/> <span class="com">// textStatus 可能为: null、"timeout"、"error"、"abort"或"parsererror"<span class="pln"><br/> <span class="com">// errorMsg 可能为: "Not Found"、"Internal Server Error"等<span class="pln"><br/><br/> <span class="com">// 提示形如:发送AJAX请求到"/index.html"时出错[404]:Not Found<span class="pln"><br/> alert<span class="pun">(<span class="pln"> <span class="str">'发送AJAX请求到"'<span class="pln"> <span class="pun">+<span class="pln"> <span class="kwd">this<span class="pun">.<span class="pln">url <span class="pun">+<span class="pln"> <span class="str">'"时出错['<span class="pln"> <span class="pun">+<span class="pln"> jqXHR<span class="pun">.<span class="pln">status <span class="pun">+<span class="pln"> <span class="str">']:'<span class="pln"> <span class="pun">+<span class="pln"> errorMsg <span class="pun">);<span class="pln"> <br/> <span class="pun">}<span class="pln"><br/><span class="pun">}<span class="pln"> <span class="pun">);</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="pln">$<span class="pun">.<span class="pln">ajax<span class="pun">(<span class="pln"> <span class="pun">{<span class="pln"><br/> url<span class="pun">:<span class="pln"> <span class="str">"myurl.php?action=list"<span class="pln"> <span class="pun">,<span class="pln"><br/> type<span class="pun">:<span class="pln"> <span class="str">"GET"<span class="pln"> <span class="pun">,<span class="pln"><br/> success<span class="pun">:<span class="pln"> <span class="kwd">function<span class="pun">(<span class="pln"> data<span class="pun">,<span class="pln"> textStatus<span class="pun">,<span class="pln"> jqXHR <span class="pun">){<span class="pln"><br/> alert<span class="pun">(<span class="str">"返回数据:"<span class="pln"> <span class="pun">+<span class="pln"> data<span class="pun">);<span class="pln"><br/> <span class="pun">}<span class="pln"> <span class="pun">,<span class="pln"><br/> error<span class="pun">:<span class="pln"> <span class="kwd">function<span class="pun">(<span class="pln">jqXHR<span class="pun">,<span class="pln"> textStatus<span class="pun">,<span class="pln"> errorMsg<span class="pun">){<span class="pln"><br/> alert<span class="pun">(<span class="str">"自己的error!"<span class="pun">);<span class="pln"> <br/> <span class="pun">}<span class="pln"><br/><span class="pun">});</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
The above is the detailed content of Take you to understand the usage examples of jQuery.ajaxSetup(). For more information, please follow other related articles on the PHP Chinese website!