jQuery - jQuery.ajax( options ) Method

The jQuery.ajax( options ) method loads a remote page using an HTTP request.
$.ajax() returns the XMLHttpRequest that it creates. In most cases you won't need that object to manipulate directly, but it is available if you need to abort the request manually.
Here is the simple syntax to use this method −
$.ajax( options )

Here is the description of all the parameters used by this method −
options − A set of key/value pairs that configure the Ajax request. All options are optional.
options − A set of key/value pairs that configure the Ajax request. All options are optional.
A Boolean indicating whether to perform the request asynchronously. The default value is true.
A callback function that is executed before the request is sent.
A callback function that executes whenever the request finishes.
A string containing a MIME content type to set for the request. The default value is application/x-www-form-urlencoded.
A map or string that is sent to the server with the request.
A function to be used to handle the raw responsed data of XMLHttpRequest. This is a pre-filtering function to sanitize the response.
A string defining the type of data expected back from the server (xml, html, json, or script).
A callback function that is executed if the request fails.
A Boolean indicating whether global AJAX event handlers will be triggered by this request. The default value is true.
A Boolean indicating whether the server should check if the page is modified before responding to the request.
Override the callback function name in a jsonp request.
A password to be used in response to an HTTP access authentication request.
A Boolean indicating whether to convert the submitted data from an object form into a query-string form. The default value is true.
A callback function that is executed if the request succeeds.
Number of milliseconds after which the request will time out in failure.
Set a local timeout (in milliseconds) for the request.
A string defining the HTTP method to use for the request (GET or POST). The default value is GET.
A string containing the URL to which the request is sent.
A username to be used in response to an HTTP access authentication request.
Callback for creating the XMLHttpRequest object. Defaults to the ActiveXObject when available (IE), the XMLHttpRequest otherwise.
Assuming we have following HTML content in result.html file −
<h1>THIS IS RESULT...</h1>

Following is a simple example a simple showing the usage of this method. Here we make use of success handler to populate returned HTML −
<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
		
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("#driver").click(function(event){
               $.ajax( {
                  url:'result.html',
                  success:function(data) {
                     $('#stage').html(data);
                  }
               });
            });
         });
      </script>
   </head>
	
   <body>
      <p>Click on the button to load result.html file:</p>
		
      <div id = "stage" style = "background-color:blue;">
         STAGE
      </div>
		
      <input type = "button" id = "driver" value = "Load Data" />
   </body>
</html>
This will produce following result −
Click on the button to load result.html file −

    27 Lectures 
     1 hours 

 Mahesh Kumar

    27 Lectures 
     1.5 hours 

 Pratik Singh

    72 Lectures 
     4.5 hours 

 Frahaan Hussain

    60 Lectures 
     9 hours 

 Eduonix Learning Solutions

    17 Lectures 
     2 hours 

 Sandip Bhattacharya

    12 Lectures 
     53 mins

 Laurence Svekis
 Print
 Add Notes
 Bookmark this page