Laboratory Experiment - 10
- Get link
- X
- Other Apps
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
General Questions on AJAX
- What is AJAX, and why is it used in web development?
- Explain the difference between synchronous and asynchronous requests in AJAX.
- What are some advantages of using AJAX in a web application?
- Can you list some common methods provided by the XMLHttpRequest object?
About the Program
- What is the purpose of the
loadDoc()
function in your program? - How does the
success
callback in the jQueryajax()
method work? - Why did you use
getJSON()
in the third section of your program? - What is the role of
parseJSON()
in the program? - How does the
xhttp.onload
function handle the AJAX response?
Error Handling
- What happens if the requested file, such as
example.txt
, is not found? How does your program handle this? - How can you ensure your AJAX requests are secure?
JSON Questions
- What is JSON, and why is it commonly used in web development?
- How does the
getJSON()
method simplify working with JSON data compared toajax()
? - What is the difference between parsing a JSON string and fetching JSON data from a file?
Code-Specific Questions
- Why is it important to use
$.ajax()
instead of plainajax()
when using jQuery? - What is the significance of
method: 'GET'
in the jQueryajax()
method? - Why is
document.getElementById()
used in the vanilla JavaScript function instead of$
? - How does the
forEach
method work when iterating through JSON data? - Why is the
innerHTML
property used to update the output in vanilla JavaScript? - What would happen if the JSON string in the
parseJSON()
example had a syntax error?
-:END:-
- Get link
- X
- Other Apps
Comments
Post a Comment