SlideShare a Scribd company logo
Ajax
Relevance of Ajax
• Normally we exchange data with server in any of following scenarios
 We click on a link to request a new page
 We submit a form to send the user data to server
• In both cases the whole webpage reloads
• AJAX is the art of exchanging data with a server, and updating parts of a web
page - without reloading the whole page.
A - Asynchronous
J - JavaScript
A - And
X - XML
“AJAX is not a new programming language, but a new way to use existing standards.”
Browser Web server
Web pages without Ajax
Browser Web server
Web pages with Ajax
How to code ajax?
Ajax is just a JavaScript function which can be called like any other
functions in JavaScript
Step 1 : create XMLHttpRequest : which is a special object to exchange data with
server
Step 2 : send server request using the above XMLHttpRequest send() method
Step 3 : Receive response from server (response can be success or error)
Step 4 : If response is success get the resposeText and display it on the html page
using JavaScript DOM manipulation
Step 5 : if repose is error acknowledge accordingly
Step 1: creating xmlHttpRequest
var xmlhttp;
if (window.XMLHttpRequest)
{ // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
Else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
Step 1: creating xmlHttpRequest
var xmlhttp;
if (window.XMLHttpRequest)
{ // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
Else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
Creating a normal variable
which will be assigned as
an object in coming lines
Step 1: creating xmlHttpRequest
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
Else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
All modern browsers
(IE7+, Firefox, Chrome,
Safari, and Opera) have a
built-in XMLHttpRequest
object.
Old versions of Internet
Explorer (IE5 and IE6) use
an ActiveX Object: for
data exchange
So this code Detects if the
browser has
XMLHTTPRequest
functionality or not
Step 1: creating xmlHttpRequest
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
Else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
If it returns true we make
the the variable xmlhttp
assigned to the
XMLHttpRequest object
Step 1: creating xmlHttpRequest
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
Else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
Else statement evaluates
true if the browser doesnt
support
XMLHttpRequest(means
the browser id old
version)
So in old versions
ActiveXobject serve the
same purpose . So we
assign xmlhttp into
ActiveX object
Step 2: Send a request to server
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
As xmlhttp is an object it will
have several methods associated
with him like open(),send() etc
Open() : Specifies the type of
request, the URL, and if the
request should be handled
asynchronously or not.
Type: GET / POST
URL : the path to page from
where we want the data from
TRUE : means data has to be
transmitted asynchronously
Step 2: Send a request to server
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
Sends the request off to the
server.
Step 3: Server response
To get the response from a server, use the responseText or responseXML
property of the XMLHttpRequest object.
« responseText : Get the response data as a string
« responseXML : Get the response data as XML
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
We receive the respose from server in a string format
which will be stored in resposeText property of
xmlhttp object
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","ajax_info.txt",false);
xmlhttp.send();
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
</script>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
Properties
Onreadystatechange
readyState
Status
responseText
responseXml
Methods
Send()
Open()
setRequestHeader()
XMLHTTP in detail
As you are well aware xmlhtp is just an object,
Which means it will have number of member
functions and properties . We will look detailed
into it
Properties
Onreadystatechange
readyState
Status
responseText
responseXml
Methods
Send()
Open()
setRequestHeader()
xmlhttp
We’ve already discussed about this
properties and methods
XMLHTTP in detail
Properties
Onreadystatechange
readyState
Status
responseText
responseXml
Methods
Send()
Open()
setRequestHeader()
xmlhttp
Stores a function (or the name of a
function) to be called automatically
each time the readyState property
changes
XMLHTTP in detail
Properties
Onreadystatechange
readyState
Status
responseText
responseXml
Methods
Send()
Open()
setRequestHeader()
xmlhttp
Holds the status of the
XMLHttpRequest.
Changes from 0 to 4:
0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is
ready
XMLHTTP in detail
Properties
Onreadystatechange
readyState
Status
responseText
responseXml
Methods
Send()
Open()
setRequestHeader()
xmlhttp
200: "OK"
404: Page not found
XMLHTTP in detail
Properties
Onreadystatechange
readyState
Status
responseText
responseXml
Methods
Send()
Open()
setRequestHeader()
xmlhttp
Adds HTTP headers to the request.
This method takes 2 parameters
header: specifies the header name
value: specifies the header value
Eg:
xmlhttp.setRequestHeader("Content-
type","application/x-www-form-
urlencoded");
XMLHTTP in detail
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
End of day
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com

More Related Content

PDF
PPTX
Introduction to ajax
PPT
Ajax Introduction
PDF
PPTX
Ajax and Jquery
PPT
An Introduction to Ajax Programming
PPT
Ajax ppt
Introduction to ajax
Ajax Introduction
Ajax and Jquery
An Introduction to Ajax Programming
Ajax ppt

What's hot (20)

PDF
PPT
Ajax Fundamentals Web Applications
PPTX
Javascript in C# for Lightweight Applications
DOCX
Copy of ajax tutorial
PDF
Pracitcal AJAX
PPT
PDF
ajax_pdf
PPT
Ajax Presentation
PDF
Beyond HTML: Tools for Building Web 2.0 Apps
PPT
Ajax
PPTX
Java Script - A New Look
PPT
2310 b 11
PPT
Pascarello_Investigating JavaScript and Ajax Security
PDF
Introduction to Ajax programming
PPT
Asp db
PPTX
XAML Data Binding in UWP
PPT
AJAX Crawl
Ajax Fundamentals Web Applications
Javascript in C# for Lightweight Applications
Copy of ajax tutorial
Pracitcal AJAX
ajax_pdf
Ajax Presentation
Beyond HTML: Tools for Building Web 2.0 Apps
Ajax
Java Script - A New Look
2310 b 11
Pascarello_Investigating JavaScript and Ajax Security
Introduction to Ajax programming
Asp db
XAML Data Binding in UWP
AJAX Crawl
Ad
Ad

Similar to Ajax (20)

PDF
Core Java tutorial at Unit Nexus
PPT
Xml http request
PPTX
AJAX.pptx
PPT
PPT
Asynchronous JavaScript & XML (AJAX)
PDF
Ajax and xml
PPT
Ajax Tuturial
PPT
Ajax.ppt
PPT
Ajax Ppt
PPT
mukesh
PPTX
Unit-5.pptx
PPT
Ajax tutorial by bally chohan
PPT
PHP - Introduction to PHP AJAX
PPTX
Web-Engineering-Lec-14 (1) .pptx
PPTX
Web-Engineering-Lec-14 (1 ).pptx
PPT
PDF
Building Applications Using Ajax
PPT
Web Programming using Asynchronous JavaX
Core Java tutorial at Unit Nexus
Xml http request
AJAX.pptx
Asynchronous JavaScript & XML (AJAX)
Ajax and xml
Ajax Tuturial
Ajax.ppt
Ajax Ppt
mukesh
Unit-5.pptx
Ajax tutorial by bally chohan
PHP - Introduction to PHP AJAX
Web-Engineering-Lec-14 (1) .pptx
Web-Engineering-Lec-14 (1 ).pptx
Building Applications Using Ajax
Web Programming using Asynchronous JavaX

More from baabtra.com - No. 1 supplier of quality freshers (20)

PPTX
Agile methodology and scrum development
PDF
Acquiring new skills what you should know
PDF
Baabtra.com programming at school
PDF
99LMS for Enterprises - LMS that you will love
PPTX
Chapter 6 database normalisation
PPTX
Chapter 5 transactions and dcl statements
PPTX
Chapter 4 functions, views, indexing
PPTX
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
PPTX
Chapter 1 introduction to sql server
PPTX
Chapter 1 introduction to sql server
Agile methodology and scrum development
Acquiring new skills what you should know
Baabtra.com programming at school
99LMS for Enterprises - LMS that you will love
Chapter 6 database normalisation
Chapter 5 transactions and dcl statements
Chapter 4 functions, views, indexing
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 1 introduction to sql server
Chapter 1 introduction to sql server

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Encapsulation theory and applications.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
A Presentation on Artificial Intelligence
PDF
Modernizing your data center with Dell and AMD
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
NewMind AI Weekly Chronicles - August'25 Week I
Reach Out and Touch Someone: Haptics and Empathic Computing
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Encapsulation theory and applications.pdf
Spectral efficient network and resource selection model in 5G networks
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Unlocking AI with Model Context Protocol (MCP)
Review of recent advances in non-invasive hemoglobin estimation
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
A Presentation on Artificial Intelligence
Modernizing your data center with Dell and AMD
Network Security Unit 5.pdf for BCA BBA.
Advanced methodologies resolving dimensionality complications for autism neur...
Encapsulation_ Review paper, used for researhc scholars
The Rise and Fall of 3GPP – Time for a Sabbatical?
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Chapter 3 Spatial Domain Image Processing.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Ajax

  • 2. Relevance of Ajax • Normally we exchange data with server in any of following scenarios  We click on a link to request a new page  We submit a form to send the user data to server • In both cases the whole webpage reloads • AJAX is the art of exchanging data with a server, and updating parts of a web page - without reloading the whole page.
  • 3. A - Asynchronous J - JavaScript A - And X - XML “AJAX is not a new programming language, but a new way to use existing standards.”
  • 4. Browser Web server Web pages without Ajax
  • 5. Browser Web server Web pages with Ajax
  • 6. How to code ajax? Ajax is just a JavaScript function which can be called like any other functions in JavaScript Step 1 : create XMLHttpRequest : which is a special object to exchange data with server Step 2 : send server request using the above XMLHttpRequest send() method Step 3 : Receive response from server (response can be success or error) Step 4 : If response is success get the resposeText and display it on the html page using JavaScript DOM manipulation Step 5 : if repose is error acknowledge accordingly
  • 7. Step 1: creating xmlHttpRequest var xmlhttp; if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } Else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
  • 8. Step 1: creating xmlHttpRequest var xmlhttp; if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } Else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } Creating a normal variable which will be assigned as an object in coming lines
  • 9. Step 1: creating xmlHttpRequest var xmlhttp; if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } Else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } All modern browsers (IE7+, Firefox, Chrome, Safari, and Opera) have a built-in XMLHttpRequest object. Old versions of Internet Explorer (IE5 and IE6) use an ActiveX Object: for data exchange So this code Detects if the browser has XMLHTTPRequest functionality or not
  • 10. Step 1: creating xmlHttpRequest var xmlhttp; if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } Else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } If it returns true we make the the variable xmlhttp assigned to the XMLHttpRequest object
  • 11. Step 1: creating xmlHttpRequest var xmlhttp; if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } Else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } Else statement evaluates true if the browser doesnt support XMLHttpRequest(means the browser id old version) So in old versions ActiveXobject serve the same purpose . So we assign xmlhttp into ActiveX object
  • 12. Step 2: Send a request to server xmlhttp.open("GET","ajax_info.txt",true); xmlhttp.send(); As xmlhttp is an object it will have several methods associated with him like open(),send() etc Open() : Specifies the type of request, the URL, and if the request should be handled asynchronously or not. Type: GET / POST URL : the path to page from where we want the data from TRUE : means data has to be transmitted asynchronously
  • 13. Step 2: Send a request to server xmlhttp.open("GET","ajax_info.txt",true); xmlhttp.send(); Sends the request off to the server.
  • 14. Step 3: Server response To get the response from a server, use the responseText or responseXML property of the XMLHttpRequest object. « responseText : Get the response data as a string « responseXML : Get the response data as XML document.getElementById("myDiv").innerHTML=xmlhttp.responseText; We receive the respose from server in a string format which will be stored in resposeText property of xmlhttp object
  • 15. <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","ajax_info.txt",false); xmlhttp.send(); document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } </script> <body> <div id="myDiv"><h2>Let AJAX change this text</h2></div> <button type="button" onclick="loadXMLDoc()">Change Content</button> </body>
  • 16. Properties Onreadystatechange readyState Status responseText responseXml Methods Send() Open() setRequestHeader() XMLHTTP in detail As you are well aware xmlhtp is just an object, Which means it will have number of member functions and properties . We will look detailed into it
  • 18. Properties Onreadystatechange readyState Status responseText responseXml Methods Send() Open() setRequestHeader() xmlhttp Stores a function (or the name of a function) to be called automatically each time the readyState property changes XMLHTTP in detail
  • 19. Properties Onreadystatechange readyState Status responseText responseXml Methods Send() Open() setRequestHeader() xmlhttp Holds the status of the XMLHttpRequest. Changes from 0 to 4: 0: request not initialized 1: server connection established 2: request received 3: processing request 4: request finished and response is ready XMLHTTP in detail
  • 21. Properties Onreadystatechange readyState Status responseText responseXml Methods Send() Open() setRequestHeader() xmlhttp Adds HTTP headers to the request. This method takes 2 parameters header: specifies the header name value: specifies the header value Eg: xmlhttp.setRequestHeader("Content- type","application/x-www-form- urlencoded"); XMLHTTP in detail
  • 22. function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","ajax_info.txt",true); xmlhttp.send(); }
  • 24. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 25. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com