jquery-ajax

jquery-ajax

AJAX is an acronym for Asynchronous JavaScript and XML, and this technology helps us download data from the server without refreshing the browser page.

If you are new to AJAX, I would recommend that you go through our Ajax tutorial before proceeding.

jQuery is a great tool that provides a rich set of AJAX methods for developing next generation web applications.

Loading simple data

It's 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 the load() method −

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

Here is a description of all options −

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

  • data - This optional parameter represents an object whose properties are serialized 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 is a callback function called after the response data has been loaded into the elements of the corresponding set. The first parameter passed to this function is the response text received from the server, and the second parameter is the status code.

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

data - This optional parameter represents an object whose properties are serialized 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 is a callback function called after the response data has been loaded into the elements of the corresponding set. The first parameter passed to this function is the response text received from the server, and the second parameter is the status code.

example

Consider the following HTML file with a little jQuery encoding:

Getting JSON data

There may be a situation where the server will return a JSON string against your request. The jQuery getJSON() utility function parses the returned JSON string and makes the resulting string available to the callback function as the first parameter for further action.

Syntax

Here is the simple syntax for getJSON() method −

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

Here is a description of all options −

  • URL - The URL of the server-side resource that the GET method contacted.

  • data is an object whose properties serve as name/value pairs used to create a query string to be appended to a URL, or a pre-formatted and encoded query string.

  • callback - the function is called after the request is completed. The data value resulting from digesting the response body as a JSON string is passed as the first parameter to this callback, and the state as the second.

URL - The URL of the server-side resource that the GET method contacted.

data is an object whose properties serve as name/value pairs used to create a query string to be appended to a URL, or a pre-formatted and encoded query string.

callback - the function is called after the request is completed. The data value resulting from digesting the response body as a JSON string is passed as the first parameter to this callback, and the state as the second.

Transferring data to the server

Many times you collect data from the user and pass it to the server for further processing. jQuery AJAX made it fairly easy to pass the collected data to the server using the data parameter of any available Ajax method.

example

This example demonstrates how user input can be passed to a web server script which will send the same result back and we will print it −

jquery ajax methods

You have seen the basic concept of AJAX using JQuery. The following table lists all the important jQuery AJAX methods that you can use based on your programming needs.

Sr.No. Methods and Description
one jQuery.ajax (options)

Load a remote page with an HTTP request.

2 jQuery.ajaxSetup (options)

Configure global settings for AJAX requests.

3 jQuery.get(url, [data], [callback], [type])

Load a remote page with an HTTP GET request.

4 jQuery.getJSON(url, [data], [callback])

Load the JSON data with an HTTP GET request.

five jQuery.getScript(url, [callback])

Downloads and executes a JavaScript file using an HTTP GET request.

6 jQuery.post(url, [data], [callback], [type])

Load a remote page with an HTTP POST request.

7 load(url, [data], [callback])

Load the HTML from the remote file and paste it into the DOM.

8 serialize()

Serializes a set of input elements into a string of data.

nine serializeArray()

Serializes all forms and form elements like the .serialize() method but returns a JSON data structure to work with for you.

Load a remote page with an HTTP request.

Configure global settings for AJAX requests.

Load a remote page with an HTTP GET request.

Load the JSON data with an HTTP GET request.

Downloads and executes a JavaScript file using an HTTP GET request.

Load a remote page with an HTTP POST request.

Load the HTML from the remote file and paste it into the DOM.

Serializes a set of input elements into a string of data.

Serializes all forms and form elements like the .serialize() method but returns a JSON data structure to work with for you.

jQuery AJAX Events

You can call various jQuery methods during the life cycle of making an AJAX call. Based on different events/stages, the following methods are available −

You can go through all AJAX Events .

Attach a function that will be executed whenever the AJAX request completes.

Attach a function that will be executed whenever an AJAX request starts and none of them are already active.

Attach a function that will be executed whenever an AJAX request fails.

Attach a function that must be executed before the AJAX request is sent.

Attach a function that will be executed whenever all AJAX requests have ended.

Attach a function that will be executed whenever the AJAX request succeeds.

 l