jQuery ajaxSetup()Method
Examples
Set the 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 Yourself »
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 with one or more name/value pairs.
The table below lists the possible name/value pairs:
| Name | Value/Description |
|---|---|
| async | A boolean value indicating whether the request is handled asynchronously. Default is true. |
| beforeSend(xhr) | A function to run before the request is sent. |
| cache | A boolean value indicating whether the browser caches the requested pages. Default is true. |
| complete(xhr,status) | A function to run when the request is complete (called after the request succeeds or fails, i.e., after the success and error functions). |
| contentType | The content type used when sending data to the server. 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) | A function used to process the raw response data of an 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 value specifying whether to trigger global AJAX event handlers for the request. Default is true. |
| ifModified | A boolean value specifying whether the request is successful only if the response has changed since the last request. Default is false. |
| jsonp | A string to override the callback function in a jsonp. |
| jsonpCallback | Specifies the name of the callback function in a jsonp. |
| password | Specifies the password to be used in an HTTP access authentication request. |
| processData | A boolean value specifying whether the data sent with the request is converted to a query string. Default is true. |
| scriptCharset | Specifies the character set for 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 value specifying 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 to. Default is the current page. |
| username | Specifies the username to be used in an HTTP access authentication request. |
| xhr | A function for creating an XMLHttpRequest object. |
More Examples
Specify the error handler for AJAX requests
How to use the error setting to specify the error handler for AJAX requests.
Other Extensions
jQuery AJAX Methods