Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery ajaxSetup() method

jQuery ajaxSetup() method

jQuery AJAX methods jQuery AJAX methods

Example

Set a default URL and success function for all AJAX requests:

$

(

"

button

"

)

.

click

(

function

(

)

{

$.

ajaxSetup

(

{

url

:

"

demo_ajax_load.txt

"

,

success

:

function

(

result

)

{

$

(

"

div

"

)

.

html

(

result

)

;

}

}

)

; $.

ajax

(

)

;

}

)

;

Try it »


Definition and Usage

The ajaxSetup() method sets default values for future AJAX requests.


Syntax

$.ajaxSetup(

{name:value, name:value, ... }

)

This parameter specifies settings for AJAX requests as one or more name/value pairs.

The table below lists the possible name/value pairs:

Name Value/Description
async A Boolean indicating whether the request is processed asynchronously. The default is true.
beforeSend(xhr) A function to run before the request is sent.
cache A Boolean indicating whether the browser caches the requested page. The default is true.
complete(xhr,status) A function to run when the request completes (called after the request succeeds or fails, that is, after the success and error functions).
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 send to the server.
dataFilter(data,type) A function used to process the raw response data of the XMLHttpRequest.
dataType The expected data type of the server response.
error(xhr,status,error) A function to run if the request fails.
global A Boolean specifying whether to trigger global AJAX event handlers for the request. The default is true.
ifModified A Boolean specifying whether the request succeeds only if the response has changed since the last request. The default is false.
jsonp A string that overrides the callback function in a jsonp request.
jsonpCallback Specifies the name of the callback function in a jsonp request.
password Specifies the password to use in an HTTP access authentication request.
processData A Boolean specifying whether the data sent with the request is converted into a query string. The default is true.
scriptCharset Specifies the character set of the request.
success(result,status,xhr) A function to run when the request succeeds.
timeout Sets the local request timeout (in milliseconds).
traditional A Boolean specifying whether to use the traditional style of parameter serialization.
type Specifies the type of the request (GET or POST).
url Specifies the URL to send the request to. The default is the current page.
username Specifies the username to use in an HTTP access authentication request.
xhr A function used to create the XMLHttpRequest object.

More examples

Specify an error handler for AJAX requests
How to use the error setting to specify an error handler for AJAX requests.


jQuery AJAX methods jQuery AJAX methods