The jQuery ajaxSetup method helps default values to set for future ajax requests. It uses an array of new settings; the default settings can be set globally. It also allows us to define globally shared properties for all the ajax requests within a single page and offers to set up default data objects to be appended to all ajax requests.
JQuery ajaxSetup Syntax
$.ajaxSetup(options)
Following table describes parameters used by ajaxSetup() method.
Name | Description |
---|---|
async | It is used to send all request in asynchronously way. Default value is true. |
beforeSend | Before sending the request, the function gets executed. |
complete | The function gets executed after the request is finished. |
contentType | It sets content type for the response object when data has to be sent to the server. The default value is “application/x-www-form-urlencoded”. |
data | The requested data that has to be sent to the server. |
dataFilter | It is used to handle the XMLHttpRequest data response. |
dataType | The type of data that is expected from the server. |
error | The function to be executed when the request gets fail. |
global | Boolean value indicates whether to trigger the global ajax event handler for this request. By default the value is true |
ifModified | It is used to check by the server that the page has been modified or no before responding to the request. |
jsonp | In jsonp request, the callback functions are override. |
password | The password is used to response to an Http access authentication request. |
processData | It is used to convert the defaultly object form into query-string form where the data is already submitted. |
success | The request is succeed when the callback function gets correctly executed. |
timeout | It is used to set the timeout for request. |
type | It is used to define the type of request to be made i.e. GET or POST. |
url | It is used to send request to the specific URL. |
username | The username is used to response to an Http access authentication request. |
xhr | It is used to create XMLHttpRequest object. |
JQuery ajaxSetup Example
Create one simple text file, save it as sample.txt and add some content in that file as shown in the below file.
AJAX is not a programming language. It is just a technique for creating better and more interactive web applications.
After creating a text file, write the coding part for the processing of the ajaxSetup() method and save it with .html extension.
<!DOCTYPE html> <head> <meta content="text/html;charset=utf-8" http-equiv="Content-Type"/> <meta content="utf-8" http-equiv="encoding"/> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $("button").click(function () { $.ajaxSetup({url:"sample.txt", dataType: "text" }); $.ajax( { success: function(result) { $("div").html(result); } }); }); }); </script> </head> <body> <div><h2>JQuery ajaxSetup Method Example</h2></div> <button>Click</button> </body> </html>
- The above program demonstrates how to access the text data file by setting url path in the program.
- $(“button”).click(function(event){ }); line defines click event which occurs when button is clicked.
- $.ajaxSetup({url:”sample.txt”, dataType: “text”}); line defines ajaxSetup() method helps default values to set for future ajax requests. The url: “sample.txt” specifies an url path of the text file and dataType: “text” specifies the type of data in the form of text.
- success: function(result) {}; statement specifies function to execute when the request succeeds.
- $(“div”).html(result); line indicates the html method.
JQuery ajaxSetup Demo
When you run the above example, you would get the following output :