json
  1. ajax-json-example

AJAX with JSON Example

  • AJAX (Asynchronous JavaScript And XML) is a technique that allows web pages to be updated asynchronously by exchanging data with a web server which can fetch data from the server without interfering with the display.
  • JSON (JavaScript Object Notation) is a lightweight format for sharing data between the web server and the client.

Syntax

The syntax for AJAX with JSON is as follows:

$.ajax({
    type: 'GET',
    url: 'url_to_data',
    dataType: 'json',
    success: function(data){
        // Code to execute if the request is successful
    },
    error: function(xhr, status, error){
        // Code to execute if the request fails
    }
});

Example

Suppose we want to fetch data from a JSON file called example.json, which contains information about employees in a company. We can use AJAX with JSON to fetch the data and display it on a webpage.

$.ajax({
    type: 'GET',
    url: 'example.json',
    dataType: 'json',
    success: function(data){
        // Code to execute if the request is successful
        var employees = data.employees; //accessing the employees array
        var output = '<ul>';
        $.each(employees, function(index, employee){
            output += '<li>' + employee.name + ': ' + employee.title + '</li>';
        });
        output += '</ul>';
        $('#employee-list').html(output);
    },
    error: function(xhr, status, error){
        // Code to execute if the request fails
        console.log(error);
    }
});

Output

The output of the above example would be a list of employees and their titles, displayed on the webpage.

John Doe: CEO
Jane Smith: CFO
Bob Johnson: CTO

Explanation

The example above makes an AJAX call to the example.json file using the HTTP GET method. The dataType property is set to json to specify that we are expecting JSON data as a response from the server.

If the AJAX call is successful, the success function is executed, which takes the data object (the JSON response from the server) and extracts the employees array from it. Then, we use the $.each() function to loop through each employee in the array and construct an HTML unordered list (<ul>) which is then added to the #employee-list element on the webpage.

If the AJAX call fails, the error function is executed, which logs the error message to the console.

Use

AJAX with JSON is useful in situations where we need to fetch data from a server and display it on a webpage without refreshing the page. This can be useful for creating dynamic interfaces, real-time updates, and responsive web applications.

Important Points

  • When using AJAX with JSON, make sure the server is returning a valid JSON response.
  • Use the dataType property to specify the expected type of response from the server.
  • Use the success function to execute code when the AJAX request is successful.
  • Use the error function to execute code when the AJAX request fails.
  • Always handle errors and exceptions properly.

Summary

In this tutorial, we learned about AJAX with JSON and how to make AJAX requests to a server to fetch and display data on a webpage. We saw how to construct an AJAX call using jQuery's $.ajax() method and how to handle the responses from the server. AJAX with JSON is a powerful technique for building dynamic web applications that can fetch and display data without refreshing the page.

Published on: