English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
$ .ajaxSetup() method sets default values for future Ajax requests.
Unless overridden by individual calls, all subsequent Ajax calls using any method will use the new settings until the next call to $ .ajaxSetup().
$.ajaxSetup({name:value, name:value, ...})
Set the default URL for all AJAX requests:
$.ajaxSetup({ url: "ajax_post.php" }); //Now, every time an Ajax request is made, the URL " ajax_post.php" will be used automatically: $.ajax({ data: {fname:"Seagull", lname:"Anna"}, method: "POST", success: function(response){ $("div").html(response); } });Test to see‹/›
Set default URL and success function for all AJAX requests:
$.ajaxSetup({ url: "ajax_data.txt", success: function(response){ $("div").html(response); } }); $("button").click(function(){ $.ajax(); });Test to see‹/›
If an Ajax request encounters an error, a notification will be displayed:
$.ajaxSetup({ url: "wrong_file.html", success: function(response){ $("div").html(response); }, error: function(xhr){ $("div").html("An error occurred: " + xhr.status + " " + xhr.statusText); } }); $("button").click(function(){ $.ajax(); });Test to see‹/›
Note:The settings specified here will affect the calls to$.ajaxor derived classes based on Ajax (such asof $.get())All calls. Since other callers (such as plugins) may expect to use normal default settings, this may lead to undesirable behavior. Therefore,It is strongly recommended not to use the $.ajaxSetup() method. Instead, set options explicitly in the call or define a simple plugin.
The parameters use one or more name:value pairs to specify the settings for the AJAX request.
Possible names: values in the following table:
Name | Value type | Description |
---|---|---|
async | Boolean | A boolean value indicating whether the request should be processed asynchronously. The default is true |
beforeSend(xhr) | Function | The function to be executed before sending the request |
cache | Boolean | A boolean value indicating whether the browser should cache the requested page. The default is true |
complete(xhr,status) | Function | The function to be executed after the request is completed (after both the success and error functions) |
contentType | Boolean or string | The content type to be used when sending data to the server. The default value is: 'application' / x-www-form-urlencoded |
context | Plain object | Specifies the 'this' value for all AJAX-related callback functions |
data | PlainObject or String or Array | Specifies the data to be sent to the server |
dataFilter(data,type) | Function | The function used to process the original response data of XMLHttpRequest |
dataType | String | The expected data type of the server response |
error(xhr,status,error) | Function | The function to be executed when the request fails |
global | Boolean | A boolean value specifying whether to trigger the global AJAX event handler for the request. The default is true |
ifModified | Boolean | A boolean value specifying that the request should only be successful if the response has changed since the last request. The default value is: false. |
jsonp | String or boolean | Overwrites the callback function in jsonp requests |
jsonpCallback | Function | Specifies the name of the callback function in jsonp requests |
method | String | Specifies the HTTP method to be used for the request (GET or POST). The default is GET |
password | String | Specifies the password used in HTTP access authentication requests |
processData | Boolean | A boolean value specifying whether the data accompanying the request should be converted to a query string. The default is true |
scriptCharset | String | Specify the character set of the request |
statusCode | Plain object | An object that will be called with the numeric HTTP code and function when the response has the corresponding code$.ajax({ statusCode:{ 404:function() alert('Page not found'); } } }); |
success(response,status,xhr) | Function | Function to be executed when the request is successful |
timeout | Number | Local timeout of the request (in milliseconds) |
traditional | Boolean | A boolean value specifying whether to use traditional parameter serialization style |
type | String | methodalias. If you are using1.9In jQuery versions prior to .0, you should use type |
url | String | Specify the URL to which the request is to be sent. The default is the current page |
username | String | Specify the username to be used in HTTP access authentication requests |
xhr | Function | Function to create XMLHttpRequest objects |