Why to study Web Technology ?

Learning Web Technology is crucial for engineering students because it equips them with essential skills to design and develop modern, user-friendly web applications. Understanding web technologies enables them to create interactive and responsive interfaces, integrate with back-end systems, and solve real-world problems. It also opens up diverse career opportunities in a rapidly growing industry, fostering innovation and adaptability in the ever-evolving digital landscape.

Laboratory Experiment - 10

10. Develop a JavaScript program with Ajax (with HTML/CSS) for:

a. Use ajax() method (without Jquery) to add the text content from the text file by sending ajax request.

b. Use ajax() method (with Jquery) to add the text content from the text file by sending ajax request.

c. Illustrate the use of getJSON() method in jQuery

d. Illustrate the use of parseJSON() method to display JSON values.


<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width,initial-scale=1.0">

    <title>Ajax Examples</title>

    <style>

        body {

            font-family: Arial,sans-serif;

            background-color: #f9f9f9;

            margin: 20px;

        }

        .container {

            max-width: 800px;

            margin: 0 auto;

        }

        button {

            padding: 10px 20px;

            background-color: #007BFF;

            color: #fff;

            border: none;

            border-radius: 5px;

            cursor: pointer;

            margin: 10px 0;

        }

        button:hover {

            background-color: #0056b3;

        }

        .output {

            margin: 20px 0;

            padding: 10px;

            border: 1px solid #ddd;

            background: #fff;

        }

    </style>

</head>

<body>

    <div class="container">

        <h1>Ajax Examples</h1>

       

        <h2>a. JavaScript `ajax()` Request</h2>

        <button onclick="loadDoc()">Load Text (Jscript) </button>

        <div class="output" id="outputJS">Text content will be loaded here.</div>

 

 

        <h2>b. jQuery `ajax()` Request</h2>

        <button id="loadTextJQuery">Load Text (jQuery)</button>

        <div id="outputJQuery" class="output">Text content will be loaded here.</div>

 

        <h2>c. Using `getJSON()`in jQuery</h2>

        <button id="loadJSONJQuery">Load JSON Data</button>

        <div id="outputJSONJQuery" class="output">JSON data will be loaded here.</div>

 

        <h2>d. Using `parseJSON()`</h2>

        <button id="loadJSONParse">Parse JSON Data</button>

        <div id="outputJSONParse" class="output">Parsed JSON data will be displayed here.</div>

    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

    <script>

        // a. AJAX using JavaScript

        function loadDoc() {

            const xhttp = new XMLHttpRequest();

            xhttp.onload = function() {

                document.getElementById("outputJS").innerHTML = this.responseText;

            }

            xhttp.open("GET","example.txt");

            xhttp.send();

        }

 

        // b. AJAX using jQuery's ajax() method

        $('#loadTextJQuery').click(function () {

            $.ajax({

            url: 'example.txt', // Specify the text file to load

            method: 'GET',

            success: function (data) {

                $('#outputJQuery').text(data);

                },

            error: function () {

                $('#outputJQuery').text('Failed to load content.');

            }

            });

        });

 

 

        // c. Using getJSON() to load JSON data

        $('#loadJSONJQuery').click(function () {

            $.getJSON('example.json', function (data) {

                let output = '<ul>';

                data.forEach(function (item) {

                    output += `<li>${item.name} - ${item.age}</li>`;

                });

            output += '</ul>';

            $('#outputJSONJQuery').html(output);

            });

        });

 

        // d. Using parseJSON() to parse JSON data

        $('#loadJSONParse').click(function () {

            const jsonString = '{"students":[{"name":"John","age":20},{"name":"Jane","age":22}]}';

            const parsedData = $.parseJSON(jsonString);

            let output = '<ul>';

            parsedData.students.forEach(function (student) {

                output += `<li>${student.name} - ${student.age}</li>`;

            });

            output += '</ul>';

            $('#outputJSONParse').html(output);

        });

 

    </script>

</body>

</html>


Input Files:

1. example.txt

This is the content of the text file loaded using AJAX.

2. example.json

[
    { "name": "John", "age": 20 },
    { "name": "Jane", "age": 22 },
    { "name": "Alice", "age": 19 }
]

Output:


Viva Questions:

General Questions on AJAX

  1. What is AJAX, and why is it used in web development?
  2. Explain the difference between synchronous and asynchronous requests in AJAX.
  3. What are some advantages of using AJAX in a web application?
  4. Can you list some common methods provided by the XMLHttpRequest object?

About the Program

  1. What is the purpose of the loadDoc() function in your program?
  2. How does the success callback in the jQuery ajax() method work?
  3. Why did you use getJSON() in the third section of your program?
  4. What is the role of parseJSON() in the program?
  5. How does the xhttp.onload function handle the AJAX response?

Error Handling

  1. What happens if the requested file, such as example.txt, is not found? How does your program handle this?
  2. How can you ensure your AJAX requests are secure?

JSON Questions

  1. What is JSON, and why is it commonly used in web development?
  2. How does the getJSON() method simplify working with JSON data compared to ajax()?
  3. What is the difference between parsing a JSON string and fetching JSON data from a file?

Code-Specific Questions

  1. Why is it important to use $.ajax() instead of plain ajax() when using jQuery?
  2. What is the significance of method: 'GET' in the jQuery ajax() method?
  3. Why is document.getElementById() used in the vanilla JavaScript function instead of $?
  4. How does the forEach method work when iterating through JSON data?
  5. Why is the innerHTML property used to update the output in vanilla JavaScript?
  6. What would happen if the JSON string in the parseJSON() example had a syntax error?

-:END:-

Comments

Popular posts from this blog

Why to study Web Technology ?