Home > Web Front-end > JS Tutorial > Take you to understand the usage examples of jQuery.ajaxSetup()

Take you to understand the usage examples of jQuery.ajaxSetup()

巴扎黑
Release: 2017-07-03 11:12:45
Original
981 people have browsed it

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.

Syntax

jQuery 1.1 Add the static function.

jQuery.ajaxSetup( settings )
Copy after login

Parameters

ParametersDescription
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:

NameValue/Description
asyncBoolean value indicating whether the request is processed asynchronously. The default is true.
beforeSend(xhr)Function to run before sending the request.
cacheBoolean 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).
contentTypeThe content type used when sending data to the server. The default is: "application/x-www-form-urlencoded".
contextSpecifies the "this" value for all AJAX-related callback functions.
dataSpecifies the data to be sent to the server.
dataFilter(data,type)Function used to process XMLHttpRequest raw response data.
dataTypeThe data type of the expected server response.
error(xhr,status,error)Function to run if the request fails.
globalBoolean value, specifies whether to trigger the global AJAX event handler program for the request. The default is true.
ifModifiedBoolean value that specifies whether the request succeeds only if the response has changed since the last request. The default is false.
jsonpOverride the string of the callback function in a jsonp.
jsonpCallbackSpecifies the name of the callback function in a jsonp.
passwordSpecifies the password used in HTTP access authentication requests.
processDataBoolean value that specifies whether the data sent through the request is converted into a query string. The default is true.
scriptCharsetSpecifies the requested character set.
success(result,status,xhr)Function that runs when the request succeeds.
timeoutSet the local request timeout (in milliseconds).
traditionalBoolean value that specifies whether to use the traditional style of parameter serialization.
typeSpecifies the type of request (GET or POST).
urlSpecifies the URL to send the request. The default is the current page.
usernameSpecifies the username used in HTTP access authentication requests.
xhrFunction used to create XMLHttpRequest objects.

Return value

jQuery.ajaxSetup()The function has no return value, or its return value is undefined.

Example & Description

<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">&#39;发送AJAX请求到"&#39;<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">&#39;"时出错[&#39;<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">&#39;]:&#39;<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>
Copy after login
<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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template