ajaxSetup() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:ajaxSetup

Description

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

Syntax

$.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 valuewhether the browser should cache the requested pages. Default is true
complete(xhr,status) functionrun when the request is finished (after success and error functions)
contentTypestring content type used when sending data to the server. Default is: "application/x-www-form-urlencoded"
contextcontext"this" value for all AJAX related callback functions
data data data to be sent to the server
dataFilter(data,type) functionused 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 valuewhether 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 stringa password to be used in an HTTP access authentication request.
processDataBoolean 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
timeoutnumber local timeout in milliseconds for the request
traditionalBoolean value specifying whether to use the traditional style of param serialization
type string type of request. (GET or POST)
urlstring URL to send the request to. Default is the current page
username string a username to be used in an HTTP access authentication request
xhrfunction used for creating the XMLHttpRequest object

The following code shows how to set the default URL and success function for all AJAX requests:

Demo Code

ResultView the demo in separate window

<!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>

Related Tutorials