FSD Module5 PPT
FSD Module5 PPT
FSD Module5 PPT
AJAX, which stands for asynchronous JavaScript and XML, is a set of technologies used
on the client-side to send and retrieve data from the server asynchronously.
DOM
XML or JSON
Django
XMLHttpRequest
Javascript
Javascript
JavaScript is a lightweight, cross-platform, single-
threaded, and interpreted compiled programming language. It is also
known as the scripting language for webpages. It is well-known for the
development of web pages, and many non-browser environments also
use it.
onmouseout The user moves the mouse away from an HTML element
JSON
JavaScript Object Notation
JSON is a lightweight format for storing and transporting data
JSON is often used when data is sent from a server to a web page
JSON is "self-describing" and easy to understand
Ajax workflow
1. user clicks, invoking an event handler
2. handler's code creates an XMLHttpRequest object
3. XMLHttpRequest object requests page from server
4. server retrieves appropriate data, sends it back
5. XMLHttpRequest fires an event when data arrives
this is often called a callback
you can attach a handler function to this event
6. your callback event handler processes the data and displays it
XMLHttpRequest Methods
JQuery
jQuery is a lightweight, "write less, do more", JavaScript library.
jQuery takes a lot of common tasks that require many lines of JavaScript code
to accomplish, and wraps them into methods that you can call with a single line
of code.
jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX
calls and DOM manipulation.
The jQuery library contains the following features:
• HTML/DOM manipulation
• CSS manipulation
• HTML event methods
• Effects and animations
• AJAX
• Utilities
Jquery Syntax
Jquery Selectors
Jquery selectors
Basic
Pure java script selectors
$(“*”)
$(“tag”)
getElementById()
$
JQuery- $()
(“*.class”)
$(“#id”)
Jquery Selectors
Jquery Selectors
var xhr = new XMLHttpRequest();
xhr.open('GET', 'example.com/data', true);
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 300) {
document.getElementById('result').innerHTML = xhr.responseText; // Update
element with id 'result'
} else {
// Handle errors
}
};
xhr.onerror = function() {
// Handle errors
};
xhr.send();
$.ajax({
url: 'example.com/data',
method: 'GET',
success: function(response) {
$('#result').html(response); // Update element with id
'result'
},
error: function(xhr, status, error) {
// Handle errors
}
});
Jquery Example
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
</body>
</html>
Jquery Demo
Jquery
<button>Send an HTTP GET request to a page and get the result back</button>
</body>
</html>
Jquery post example
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post("demo_test_post.asp",
{
name: "Donald Duck",
city: "Duckburg"
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</head>
<body>
<button>Send an HTTP POST request to a page and get the result back</button>
</body>
</html>
REST APIs
Building Rest API using Dijango
Lab Program