Professional Documents
Culture Documents
Ajax, which stands for Asynchronous JavaScript and XML, is a set of techniques for creating
highly interactive web sites and web applications. The idea is to make what’s on the Web appear
to be local by giving you a rich user experience, offering you features that usually only appear in
desktop applications.
The emphasis in Ajax applications is to update the web page, using data fetched from the
Internet, without refreshing the web page in the browser. You saw an example of that with
Google Suggest, where a drop-down list appears in the browser without a page refresh.
The term “Ajax” was created by Jesse James Garrett, president of Adaptive Path, in a February
18, 2005 article collecting the technologies that already existed, and which make up Ajax, under
one umbrella term.
We aren’t getting enough orders on our Web site,” storms the CEO.
“People just don’t like clicking all those buttons and waiting for a new page all the time. It’s too
distracting.”
“How about a simpler solution?” you ask. “What if people could stay on the same page and just
drag the items they want to buy to a shopping cart? No page refreshes, no fuss, no muss.”
“You mean people wouldn’t have to navigate from page to page to add items to a shopping cart
and then check out? Customers could do everything on a single Web page?”
“Yep,” you say. “And that page would automatically let our software on the server know what
items the customer had purchased — all without having to reload the Web page.”
“I love it!” the CEO says. “What’s it called?”
“Ajax,” you say.
Welcome to the world of Ajax, the technology that lets Web software act like desktop software.
One of the biggest problems with traditional Web applications is that they have that “Web” feel
— you have to keep clicking buttons to move from page to page, and watch the screen flicker as
your browser loads
a new Web page. Ajax is here to take care of that issue, because it enables you grab data from
the server without reloading new pages into the browser.
AJAX allows web pages to be updated asynchronously by exchanging data with a web server
behind the scenes. This means that it is possible to update parts of a web page, without reloading
the whole page.
AJAX stands for Asynchronous JavaScript and XML. AJAX is a new technique for creating
better, faster, and more interactive web applications with the help of XML, HTML, CSS, and
Java Script.
Ajax uses XHTML for content, CSS for presentation, along with Document Object
Model and JavaScript for dynamic content display.
Conventional web applications transmit information to and from the sever using
synchronous requests. It means you fill out a form, hit submit, and get directed to a new
page with new information from the server.
With AJAX, when you hit submit, JavaScript will make a request to the server, interpret
the results, and update the current screen. In the purest sense, the user would never know
that anything was even transmitted to the server.
XML is commonly used as the format for receiving server data, although any format,
including plain text, can be used. XML was designed to store and transport
data.AJAX is a web browser technology independent of web server software.
A user can continue to use the application while the client program requests information
from the server in the background.
Intuitive and natural user interaction. Clicking is not required, mouse movement is a
sufficient event trigger.
Data-driven as opposed to page-driven
AJAX cannot work independently. It is used in combination with other technologies to create
interactive webpages.
JavaScript
Allows for a clear separation of the presentation style from the content and may be
changed programmatically by JavaScript
XMLHttpRequest
EXAMPLE:
<!DOCTYPE html>
<html>
<body>
<h1>The XMLHttpRequest Object</h1>
<p id="demo">Let AJAX change this text.</p>
<button type="button" onclick="loadDoc()">Change Content</button>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
</script>
</body>
</html>
The XMLHttpRequest Object
The XMLHttpRequest object can be used to exchange data with a server behind the scenes. This
means that it is possible to update parts of a web page, without reloading the whole page.
All modern browsers (Chrome, Firefox, IE7+, Edge, Safari Opera) have a built-in
XMLHttpRequest object.
Method Description
new XMLHttpRequest() Creates a new XMLHttpRequest object
Property Description
Synchronous requests
var ajax = new XMLHttpRequest();
ajax.open("GET", url, false);
ajax.send(null);
Download the Windows version of XAMPP and begin installation. Execute the installer when
prompted. You can select only Apache if all you need is a web server. However if you are
planning on using a database, you may want to select MySQL as well.
Continue through the installation and click “Finish” when complete. By default, the XAMPP
control panel will be launched.
If you navigate to “127.0.0.1” or “localhost” in your web browser, you should see the XAMPP
configuration page.
To create a new webpage, the procedure is the same. Open up notepad and create a sample
HTML file. Name it “hello.html.”
Save it in the document root located in c:\xampp\htdocs\.
Example Program
Two files: 1. Java4s.txt
2. hello.html
Save both files on e:/xampp/htdocs
a.onreadystatechange=function()
3. Activated only when the
{
response is received and the
if (a.readyState==4 && a.status==200)
ready state changes. Displays
{
the result.
document.getElementById("myDiv").innerHTML=a.responseText;
}
}
</head>
<body>
</body>
</html>
Once the document loaded then immediately we will be able to see one button Change
Content , and see i have been given onclick=”fun1()” [ line number 33 ] means once we click
on this button controller will go to line number 4 and will starts execute that fun1()
At line number 6, i have taken one variable with name a
For any ajax program, we must create one request object to send our ajax request to the
server, that ajax object is nothing but XMLHttpRequest object
See line number 8, i have written window.XMLHttpRequest means am checking whether my
browser supports XMLHttpRequest object or not, if yes assigning XMLHttpRequest object
into a [ a=new XMLHttpRequest(); ] else i mean if our web browser doesnt
supports XMLHttpRequest object then we can assign ActiveXObject [ which supports old
web browsers ] into our variable a [ a=new ActiveXObject(“Microsoft.XMLHTTP”); ]
So from line numbers 8 -15 request object creation work been done
Now controller directly jumps to line number 25, and opens the connection and send the
request to java4s.txt (this is my notepad file), see actually the 3rd parameter i have given
as true means Asynchronous data transfer will be activated
Finally request will be sent in line number 26, that’s it.
If server sends the response back to our application then controller will automatically execute
from line number 17 – 23, you may get one doubt like why its not executed initially… ? there
is a reason actually this a.onreadystatechange=function() executes only when we get the
response from the server, hope you are clear 😉
<!DOCTYPE html>
<html>
<body>
<div id="demo">
<button type="button"
onclick="loadDoc('ajax_info.txt', myFunction)">Change Content
</button>
</div>
<script>
function myFunction(xhttp) {
document.getElementById("demo").innerHTML =
xhttp.responseText;
}
</script>
</body>
</html>
Property Description
responseText get the response data as a string
responseXML get the response data as XML data
Example
document.getElementById("demo").innerHTML = xhttp.responseText;
The responseXML Property
The XML HttpRequest object has an in-built XML parser.
The responseXML property returns the server response as an XML DOM object.