Ajax With JQuery
Ajax With JQuery
Introduction
• AJAX is the art of exchanging data with a server, and updating
parts of a web page - without reloading the whole page.
• AJAX = Asynchronous JavaScript and XML.
• In short; AJAX is about loading data in the background and
display it on the webpage, without reloading the whole page.
• Examples of applications using AJAX: Gmail, Google Maps,
Youtube, and Facebook tabs.
• AJAX is a developer's dream, because you can:
• Update a web page without reloading the page
• Request data from a server - after the page has loaded
• Receive data from a server - after the page has loaded
• Send data to a server - in the background
• AJAX is not a programming
language.
• AJAX just uses a
combination of:
• A browser built-in
XMLHttpRequest object (to
request data from a web
server)
• JavaScript and HTML DOM
(to display or use the data)
• 1.An event occurs in a web page (the
page is loaded, a button is clicked)
• 2. An XMLHttpRequest object is created
by JavaScript
• 3. The XMLHttpRequest object sends a
request to a web server
• 4. The server processes the request
• 5. The server sends a response back to
the web page
• 6. The response is read by JavaScript
• 7. Proper action (like page update) is
performed by JavaScript
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("demo_test.txt");
});
});
</script>
</head>
<body>
</body>
</html>
• jQuery provides several methods for AJAX functionality.
• With the jQuery AJAX methods, you can request text,
HTML, XML, or JSON from a remote server using both
HTTP Get and HTTP Post - And you can load the
external data directly into the selected HTML elements
of your web page!
• The required URL parameter specifies the URL you wish to load.
• The optional data parameter specifies a set of querystring key/value
pairs to send along with the request.
• The optional callback parameter is the name of a function to be
executed after the load() method is completed.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("demo_test.txt #p1");
});
});
</script>
</head>
<body>
</body>
</html>
• The optional callback parameter specifies a callback function to run
when the load() method is completed. The callback function can have
different parameters:
<button>Send an HTTP GET request to a page and get the result back</button>
</body>
</html>
• jQuery $.post() Method
• The $.post() method requests data from the server using an HTTP POST
request.
• Syntax:
• $.post(URL,data,callback);
• The required URL parameter specifies the URL you wish to request.
• The optional data parameter specifies some data to send along with the
request.
• The optional callback parameter is the name of a function to be executed if
the request succeeds
The first parameter of $.post() is the URL we wish to request
("demo_test_post.asp").
Then we pass in some data to send along with the request (name and city)..
<!DOCTYPE html>
<html><head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.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>
Advantages of AJAX