Professional Documents
Culture Documents
Introduction To AJAX Comparison: 3 April 2021
Introduction To AJAX Comparison: 3 April 2021
Dr.V.Mareeswari
Assistant Professsor(Sr)
School of Information Technology and Engineering
VIT,Vellore
Cabin No:SJT 210-A30
Introduction to AJAX
AJAX = Asynchronous JavaScript and XML.
Comparison
AJAX is a technique for creating fast and dynamic web pages.
AJAX allows web pages to be updated asynchronously by exchanging small
amounts of data with the server behind the scenes. This means that it is
possible to update parts of a web page, without reloading the whole page.
Classic web pages, (which do not use AJAX) must reload the entire page if
the content should change.
Examples of applications using AJAX: Google Maps, Google Suggest, Gmail,
Youtube, Flickr, and Facebook tabs.
AJAX meant to increase the web page's interactivity, speed, and
usability.
Ajax is not a programming language or a tool, but a concept. Ajax is
a client-side script that communicates to and from a server/database
3
without the need for a postback or a complete page refresh.
V.Mareeswari / AP (Sr) / SITE 4/3/2021 4 V.Mareeswari / AP (Sr) / SITE 4/3/2021
Output
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change
Content</button>
</body> </html>
C:/wamp/www/AJAXTextFile.txt
AJAX is not a programming language.
AJAX is a technique for accessing web servers from a web page.
AJAX stands for Asynchronous JavaScript And XML.
If the response from the server is XML, and you want to parse it as an
The onreadystatechange event
XML object, use the responseXML property: When a request to a server is sent, we want to perform some
Request the file cd_catalog.xml and parse the response: actions based on the response.
The onreadystatechange event is triggered every time the
xmlDoc=xmlhttp.responseXML; readyState changes.
txt=“ “; The readyState property holds the status of the XMLHttpRequest.
x=xmlDoc.getElementsByTagName("ARTIST"); Three important properties of the XMLHttpRequest object:
for (i=0;i<x.length;i++)
{ xmlhttp.onreadystatechange=function() {
txt=txt + x[i].childNodes[0].nodeValue + "<br>"; if (xmlhttp.readyState==4 && xmlhttp.status==200) {
} document.getElementById("myDiv").innerHTML=
document.getElementById("myDiv").innerHTML=txt; xmlhttp.responseText; } }
Note: The onreadystatechange event is triggered five times (0-4), one
time for each change in readyState.
17 V.Mareeswari / AP (Sr) / SITE 4/3/2021 18 V.Mareeswari / AP (Sr) / SITE 4/3/2021
Exercises
The following example will demonstrate how a web page can
communicate with a web server while a user type characters in an
input field:
Exercise
33 Dr. V.Mareeswari / AP(Sr) / SITE / VIT 34 Dr. V.Mareeswari / AP(Sr) / SITE / VIT
$(document).ready(function () { $(document).ready(function () {
$('#ajaxBtn').click(function(){ $('#ajaxBtn').click(function(){
$.ajax('/jquery/getjsondata', { $.ajax('/jquery/submitData', {
dataType: 'json', // type of response data type: 'POST', // http method
timeout: 500, // timeout milliseconds data: { myData: ' This is mareeswari data.' }, // data to submit
success: function (data,status,xhr) { // success callback function success: function (data, status, xhr) {
$('p').append(data.firstName + ' ' + data.middleName + ' ' + $('p').append('status: ' + status + ', data: ' + data); },
data.lastName); }, error: function (jqXhr, textStatus, errorMessage) {
error: function (jqXhr, textStatus, errorMessage) { // error callback $('p').append('Error: ' + errorMessage); }});}); });
$('p').append('Error: ' + errorMessage); }}); });});
In the options parameter, we have specified a type
option as a POST, so ajax() method will send http
POST request. Also, we have specified data option
as a JSON object containing data which will be
35 Dr. V.Mareeswari / AP(Sr) / SITE / VIT 36 Dr. V.Mareeswari / AP(Sr) / SITE submitted
/ VIT to the server.
The jQuery get() method sends asynchronous http GET request to the
server and retrieves the data. Ajax will not die. Traditional Ajax refers to
Syntax: $.get(url, [data],[callback]); XMLHttpRequest (XHR), Future It has now been
The jQuery post() method sends asynchronous http POST request to the replaced by fetch.
server to submit the data to the server and get the response.
Syntax: $.post(url,[data],[callback],[type]);
The jQuery load() method allows HTML or text content to be loaded
from a server and added into a DOM element.
The jQuery load() method allows HTML or text content to be loaded
from a server and added into a DOM element.
Syntax: $.load(url,[data],[callback]);
37 Dr. V.Mareeswari / AP(Sr) / SITE / VIT 38 Dr. V.Mareeswari / AP(Sr) / SITE / VIT
AJAX Security
AJAX Security AJAX-based Web applications use the same server-side
security schemes of regular Web applications.
You specify authentication, authorization, and data protection requirements
in your web.xml file (declarative) or in your program (programmatic).
AJAX-based Web applications are subject to the same security threats as
regularWeb applications.
AJAX Security: Client Side
JavaScript code is visible to a user/hacker. Hacker can use JavaScript code for
inferring server-side weaknesses.
JavaScript code is downloaded from the server and executed ("eval") at the
client and can compromise the client by mal-intended code.
Downloaded JavaScript code is constrained by the sand-box security model
and can be relaxed for signed JavaScript.
39 Dr. V.Mareeswari / AP(Sr) / SITE / VIT