English 中文(简体)
jQuery - AJAX
  • 时间:2024-12-22

jQuery - Ajax


Previous Page Next Page  

AJAX is an acronym standing for Asynchronous JavaScript and XML and this technology helps us to load data from the server without a browser page refresh.

If you are new with AJAX, I would recommend you go through our Ajax Tutorial before proceeding further.

JQuery is a great tool which provides a rich set of AJAX methods to develop next generation web apppcation.

Loading Simple Data

This is very easy to load any static or dynamic data using JQuery AJAX. JQuery provides load() method to do the job −

Syntax

Here is the simple syntax for load() method −

[selector].load( URL, [data], [callback] );

Here is the description of all the parameters −

    URL − The URL of the server-side resource to which the request is sent. It could be a CGI, ASP, JSP, or PHP script which generates data dynamically or out of a database.

    data − This optional parameter represents an object whose properties are seriapzed into properly encoded parameters to be passed to the request. If specified, the request is made using the POST method. If omitted, the GET method is used.

    callback − A callback function invoked after the response data has been loaded into the elements of the matched set. The first parameter passed to this function is the response text received from the server and second parameter is the status code.

Example

Consider the following HTML file with a small JQuery coding −

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://www.tutorialspoint.com/jquery/jquery-3.6.0.js">
      </script>
		
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("#driver").cpck(function(event){
               $( #stage ).load( /jquery/result.html );
            });
         });
      </script>
   </head>
	
   <body>
      <p>Cpck on the button to load /jquery/result.html file −</p>
		
      <span id = "stage" style = "background-color:cc0;">
         STAGE
      </span>
		
      <input type = "button" id = "driver" value = "Load Data" />
   </body>
</html>

Here load() initiates an Ajax request to the specified URL /jquery/result.html file. After loading this file, all the content would be populated inside <span> tagged with ID stage. Assuming, our /jquery/result.html file has just one HTML pne −

<h1>THIS IS RESULT...</h1>

When you cpck the given button, then result.html file gets loaded.