Javascript examples for jQuery Method and Property:ajaxSetup
The ajaxSetup() method sets default values for future AJAX requests.
$.ajaxSetup({name:value, name:value, ... })
The parameters specifies the settings for AJAX requests with one or more name/value pairs.
Possible names/values in the table below:
Name | Value | Description |
---|---|---|
async | Boolean value | whether the request should be handled asynchronous. Default is true |
beforeSend(xhr) | function | run before the request is sent |
cache | Boolean value | whether the browser should cache the requested pages. Default is true |
complete(xhr,status) | function | run when the request is finished (after success and error functions) |
contentType | string | content type used when sending data to the server. Default is: "application/x-www-form-urlencoded" |
context | context | "this" value for all AJAX related callback functions |
data | data | data to be sent to the server |
dataFilter(data,type) | function | used to handle the raw response data of the XMLHttpRequest |
dataType | string | data type expected of the server response. |
error(xhr,status,error) | function | run if the request fails. |
global | Boolean value | whether to trigger global AJAX event handles for the request. Default is true |
ifModified | Boolean value | whether a request is only successful if the response has changed since the last request. Default is: false. |
jsonp | string | overriding the callback function in a jsonp request |
jsonpCallback | string | a name for the callback function in a jsonp request |
password | string | a password to be used in an HTTP access authentication request. |
processData | Boolean value | whether data sent with the request should be transformed into a query string. Default is true |
scriptCharset | string | the charset for the request |
success(result,status,xhr) | function | run when the request succeeds |
timeout | number | local timeout in milliseconds for the request |
traditional | Boolean value | specifying whether to use the traditional style of param serialization |
type | string | type of request. (GET or POST) |
url | string | URL to send the request to. Default is the current page |
username | string | a username to be used in an HTTP access authentication request |
xhr | function | used for creating the XMLHttpRequest object |
The following code shows how to set the default URL and success function for all AJAX requests:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $.ajaxSetup({url: "demo_ajax_load.txt", success: function(result){ $("div").html(result);}}); $.ajax();/* ww w .j a v a2 s.c o m*/ }); }); </script> </head> <body> <div><h2>Let AJAX change this text</h2></div> <button>Change Content</button> </body> </html>