JQuery ajaxSetup Example

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.

NameDescription
asyncIt is used to send all request in asynchronously way. Default value is true.
beforeSendBefore sending the request, the function gets executed.
completeThe function gets executed after the request is finished.
contentTypeIt 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”.
dataThe requested data that has to be sent to the server.
dataFilterIt is used to handle the XMLHttpRequest data response.
dataTypeThe type of data that is expected from the server.
errorThe function to be executed when the request gets fail.
globalBoolean value indicates whether to trigger the global ajax event handler for this request. By default the value is true
ifModifiedIt is used to check by the server that the page has been modified or no before responding to the request.
jsonpIn jsonp request, the callback functions are override.
passwordThe password is used to response to an Http access authentication request.
processDataIt is used to convert the defaultly object form into query-string form where the data is already submitted.
successThe request is succeed when the callback function gets correctly executed.
timeoutIt is used to set the timeout for request.
typeIt is used to define the type of request to be made i.e. GET or POST.
urlIt is used to send request to the specific URL.
usernameThe username is used to response to an Http access authentication request.
xhrIt 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 :

JQuery AjaxSetup ExampleJQuery AjaxSetup Example1

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*